3 * Copyright (C) 2007, 2008 Ecole des Mines de Nantes
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License (GPL)
7 * version 2 as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * file license.txt for more details.
20 (*****************************************************************************)
21 (* Is_xxx, categories *)
22 (*****************************************************************************)
24 (* could define a type token_class = Comment | Ident | Operator | ...
25 * update: now token_c can maybe do that.
26 * but still, sometimes tokens belon to multiple classes. Could maybe
27 * return then a set of classes.
30 let is_space = function
31 | TCommentSpace _
-> true
32 | TCommentNewline _
-> true
35 let is_whitespace = is_space
37 let is_just_comment_or_space = function
39 | TCommentSpace _
-> true
40 | TCommentNewline _
-> true
42 let is_real_comment = is_just_comment_or_space
44 let is_just_comment = function
51 let is_comment = function
53 | TCommentSpace _
| TCommentNewline _
55 | TCommentMisc _
-> true
58 (* coupling with comment_annotater_c.ml.
59 * In fact more tokens than comments are not in the ast, but
60 * they were usually temporally created by ocamllex and removed
63 let is_not_in_ast = is_comment
65 let is_fake_comment = function
66 | TCommentCpp _
| TCommentMisc _
70 let is_not_comment x
=
74 (* ---------------------------------------------------------------------- *)
76 let is_cpp_instruction = function
79 | TIfdef _
| TIfdefelse _
| TIfdefelif _
| TEndif _
80 | TIfdefBool _
| TIfdefMisc _
| TIfdefVersion _
82 | TCppDirectiveOther _
87 let is_gcc_token = function
98 (* ---------------------------------------------------------------------- *)
99 let is_opar = function
100 | TOPar _
| TOParDefine _
-> true
103 let is_cpar = function
104 | TCPar _
| TCParEOL _
-> true
108 let is_obrace = function
109 | TOBrace _
| TOBraceDefineInit _
-> true
112 let is_cbrace = function
119 (* ---------------------------------------------------------------------- *)
122 let is_eof = function
128 let is_eom = function
132 let is_statement = function
133 | Tfor _
| Tdo _
| Tif _
| Twhile _
| Treturn _
134 | Tbreak _
| Telse _
| Tswitch _
| Tcase _
| Tcontinue _
141 (* is_start_of_something is used in parse_c for error recovery, to find
142 * a synchronisation token.
144 * Would like to put TIdent or TDefine, TIfdef but they can be in the
145 * middle of a function, for instance with label:.
147 * Could put Typedefident but fired ? it would work in error recovery
148 * on the already_passed tokens, which has been already gone in the
149 * Parsing_hacks.lookahead machinery, but it will not work on the
150 * "next" tokens. But because the namespace for labels is different
151 * from namespace for ident/typedef, we can use the name for a typedef
152 * for a label and so dangerous to put Typedefident at true here.
154 * Can look in parser_c.output to know what can be at toplevel
155 * at the very beginning.
158 let is_start_of_something = function
159 | Tchar _
| Tshort _
| Tint _
| Tdouble _
| Tfloat _
| Tlong _
160 | Tunsigned _
| Tsigned _
| Tvoid _
161 | Tauto _
| Tregister _
| Textern _
| Tstatic _
162 | Tconst _
| Tvolatile _
164 | Tstruct _
| Tunion _
| Tenum _
170 let is_binary_operator = function
171 | TOrLog _
| TAndLog _
| TOr _
| TXor _
| TAnd _
172 | TEqEq _
| TNotEq _
| TInf _
| TSup _
| TInfEq _
| TSupEq _
174 | TPlus _
| TMinus _
| TMul _
| TDiv _
| TMod _
178 let is_stuff_taking_parenthized = function
188 (* used in the algorithms for "10 most problematic errors" *)
189 let is_ident_like = function
193 | TDefParamVariadic _
198 | TMacroAttrStorage _
209 (*****************************************************************************)
211 (*****************************************************************************)
213 (* Because ocamlyacc force us to do it that way. The ocamlyacc token
214 * cant be a pair of a sum type, it must be directly a sum type.
216 let info_of_tok = function
217 | TString
((string, isWchar
), i
) -> i
218 | TChar
((string, isWchar
), i
) -> i
219 | TFloat
((string, floatType
), i
) -> i
221 | TAssign
(assignOp
, i
) -> i
224 | TypedefIdent
(s
, i
) -> i
229 | TInclude
(includes
, filename
, inifdef
, i1
) -> i1
231 | TUndef
(s
, ii
) -> ii
232 | TCppDirectiveOther
(ii
) -> ii
234 | TIncludeStart
(i1
, inifdef
) -> i1
235 | TIncludeFilename
(s
, i1
) -> i1
238 | TOParDefine
(i1
) -> i1
239 | TIdentDefine
(s
, i
) -> i
240 | TCppEscapedNewline
(ii
) -> ii
241 | TDefParamVariadic
(s
, i1
) -> i1
243 | TCppConcatOp
(ii
) -> ii
245 | TOBraceDefineInit
(i1
) -> i1
249 | TMacroIdentBuilder
(s
, i
) -> i
250 | TMacroAttr
(s
, i
) -> i
251 | TMacroAttrStorage
(s
, i
) -> i
252 | TMacroStmt
(s
, i
) -> i
253 | TMacroString
(s
, i
) -> i
254 | TMacroDecl
(s
, i
) -> i
255 | TMacroDeclConst
(i
) -> i
256 | TMacroIterator
(s
,i
) -> i
257 (* | TMacroTop (s,i) -> i *)
258 | TCParEOL
(i1
) -> i1
263 | TCommentSpace
(i
) -> i
264 | TCommentNewline
(i
) -> i
265 | TCommentCpp
(cppkind
, i
) -> i
266 | TCommentMisc
(i
) -> i
268 | TCommentSkipTagStart
(i
) -> i
269 | TCommentSkipTagEnd
(i
) -> i
272 | TIfdefelse
(_
, i
) -> i
273 | TIfdefelif
(_
, i
) -> i
275 | TIfdefBool
(b
, _
, i
) -> i
276 | TIfdefMisc
(b
, _
, i
) -> i
277 | TIfdefVersion
(b
, _
, i
) -> i
352 | Tattribute
(i
) -> i
361 (* used by tokens to complete the parse_info with filename, line, col infos *)
362 let visitor_info_of_tok f
= function
363 | TString
((s
, isWchar
), i
) -> TString
((s
, isWchar
), f i
)
364 | TChar
((s
, isWchar
), i
) -> TChar
((s
, isWchar
), f i
)
365 | TFloat
((s
, floatType
), i
) -> TFloat
((s
, floatType
), f i
)
366 | TAssign
(assignOp
, i
) -> TAssign
(assignOp
, f i
)
368 | TIdent
(s
, i
) -> TIdent
(s
, f i
)
369 | TypedefIdent
(s
, i
) -> TypedefIdent
(s
, f i
)
370 | TInt
(s
, i
) -> TInt
(s
, f i
)
372 | TDefine
(i1
) -> TDefine
(f i1
)
374 | TUndef
(s
,i1
) -> TUndef
(s
, f i1
)
375 | TCppDirectiveOther
(i1
) -> TCppDirectiveOther
(f i1
)
377 | TInclude
(includes
, filename
, inifdef
, i1
) ->
378 TInclude
(includes
, filename
, inifdef
, f i1
)
380 | TIncludeStart
(i1
, inifdef
) -> TIncludeStart
(f i1
, inifdef
)
381 | TIncludeFilename
(s
, i1
) -> TIncludeFilename
(s
, f i1
)
383 | TCppEscapedNewline
(i1
) -> TCppEscapedNewline
(f i1
)
384 | TDefEOL
(i1
) -> TDefEOL
(f i1
)
386 | TCppConcatOp
(ii
) -> TCppConcatOp
(f ii
)
388 | TOParDefine
(i1
) -> TOParDefine
(f i1
)
389 | TIdentDefine
(s
, i
) -> TIdentDefine
(s
, f i
)
391 | TDefParamVariadic
(s
, i1
) -> TDefParamVariadic
(s
, f i1
)
393 | TOBraceDefineInit
(i1
) -> TOBraceDefineInit
(f i1
)
396 | TUnknown
(i
) -> TUnknown
(f i
)
398 | TMacroIdentBuilder
(s
, i
) -> TMacroIdentBuilder
(s
, f i
)
399 | TMacroAttr
(s
, i
) -> TMacroAttr
(s
, f i
)
400 | TMacroAttrStorage
(s
, i
) -> TMacroAttrStorage
(s
, f i
)
401 | TMacroStmt
(s
, i
) -> TMacroStmt
(s
, f i
)
402 | TMacroString
(s
, i
) -> TMacroString
(s
, f i
)
403 | TMacroDecl
(s
, i
) -> TMacroDecl
(s
, f i
)
404 | TMacroDeclConst
(i
) -> TMacroDeclConst
(f i
)
405 | TMacroIterator
(s
, i
) -> TMacroIterator
(s
, f i
)
406 (* | TMacroTop (s,i) -> TMacroTop (s,f i) *)
407 | TCParEOL
(i
) -> TCParEOL
(f i
)
410 | TAction
(i
) -> TAction
(f i
)
412 | TComment
(i
) -> TComment
(f i
)
413 | TCommentSpace
(i
) -> TCommentSpace
(f i
)
414 | TCommentNewline
(i
) -> TCommentNewline
(f i
)
415 | TCommentCpp
(cppkind
, i
) -> TCommentCpp
(cppkind
, f i
)
416 | TCommentMisc
(i
) -> TCommentMisc
(f i
)
418 | TCommentSkipTagStart
(i
) -> TCommentSkipTagStart
(f i
)
419 | TCommentSkipTagEnd
(i
) -> TCommentSkipTagEnd
(f i
)
421 | TIfdef
(t
, i
) -> TIfdef
(t
, f i
)
422 | TIfdefelse
(t
, i
) -> TIfdefelse
(t
, f i
)
423 | TIfdefelif
(t
, i
) -> TIfdefelif
(t
, f i
)
424 | TEndif
(t
, i
) -> TEndif
(t
, f i
)
425 | TIfdefBool
(b
, t
, i
) -> TIfdefBool
(b
, t
, f i
)
426 | TIfdefMisc
(b
, t
, i
) -> TIfdefMisc
(b
, t
, f i
)
427 | TIfdefVersion
(b
, t
, i
) -> TIfdefVersion
(b
, t
, f i
)
429 | TOPar
(i
) -> TOPar
(f i
)
430 | TCPar
(i
) -> TCPar
(f i
)
431 | TOBrace
(i
) -> TOBrace
(f i
)
432 | TCBrace
(i
) -> TCBrace
(f i
)
433 | TOCro
(i
) -> TOCro
(f i
)
434 | TCCro
(i
) -> TCCro
(f i
)
435 | TDot
(i
) -> TDot
(f i
)
436 | TComma
(i
) -> TComma
(f i
)
437 | TPtrOp
(i
) -> TPtrOp
(f i
)
438 | TInc
(i
) -> TInc
(f i
)
439 | TDec
(i
) -> TDec
(f i
)
440 | TEq
(i
) -> TEq
(f i
)
441 | TWhy
(i
) -> TWhy
(f i
)
442 | TTilde
(i
) -> TTilde
(f i
)
443 | TBang
(i
) -> TBang
(f i
)
444 | TEllipsis
(i
) -> TEllipsis
(f i
)
445 | TDotDot
(i
) -> TDotDot
(f i
)
446 | TPtVirg
(i
) -> TPtVirg
(f i
)
447 | TOrLog
(i
) -> TOrLog
(f i
)
448 | TAndLog
(i
) -> TAndLog
(f i
)
449 | TOr
(i
) -> TOr
(f i
)
450 | TXor
(i
) -> TXor
(f i
)
451 | TAnd
(i
) -> TAnd
(f i
)
452 | TEqEq
(i
) -> TEqEq
(f i
)
453 | TNotEq
(i
) -> TNotEq
(f i
)
454 | TInf
(i
) -> TInf
(f i
)
455 | TSup
(i
) -> TSup
(f i
)
456 | TInfEq
(i
) -> TInfEq
(f i
)
457 | TSupEq
(i
) -> TSupEq
(f i
)
458 | TShl
(i
) -> TShl
(f i
)
459 | TShr
(i
) -> TShr
(f i
)
460 | TPlus
(i
) -> TPlus
(f i
)
461 | TMinus
(i
) -> TMinus
(f i
)
462 | TMul
(i
) -> TMul
(f i
)
463 | TDiv
(i
) -> TDiv
(f i
)
464 | TMod
(i
) -> TMod
(f i
)
465 | Tchar
(i
) -> Tchar
(f i
)
466 | Tshort
(i
) -> Tshort
(f i
)
467 | Tint
(i
) -> Tint
(f i
)
468 | Tdouble
(i
) -> Tdouble
(f i
)
469 | Tfloat
(i
) -> Tfloat
(f i
)
470 | Tlong
(i
) -> Tlong
(f i
)
471 | Tunsigned
(i
) -> Tunsigned
(f i
)
472 | Tsigned
(i
) -> Tsigned
(f i
)
473 | Tvoid
(i
) -> Tvoid
(f i
)
474 | Tauto
(i
) -> Tauto
(f i
)
475 | Tregister
(i
) -> Tregister
(f i
)
476 | Textern
(i
) -> Textern
(f i
)
477 | Tstatic
(i
) -> Tstatic
(f i
)
478 | Tconst
(i
) -> Tconst
(f i
)
479 | Tvolatile
(i
) -> Tvolatile
(f i
)
481 | Trestrict
(i
) -> Trestrict
(f i
)
483 | Tstruct
(i
) -> Tstruct
(f i
)
484 | Tenum
(i
) -> Tenum
(f i
)
485 | Ttypedef
(i
) -> Ttypedef
(f i
)
486 | Tunion
(i
) -> Tunion
(f i
)
487 | Tbreak
(i
) -> Tbreak
(f i
)
488 | Telse
(i
) -> Telse
(f i
)
489 | Tswitch
(i
) -> Tswitch
(f i
)
490 | Tcase
(i
) -> Tcase
(f i
)
491 | Tcontinue
(i
) -> Tcontinue
(f i
)
492 | Tfor
(i
) -> Tfor
(f i
)
493 | Tdo
(i
) -> Tdo
(f i
)
494 | Tif
(i
) -> Tif
(f i
)
495 | Twhile
(i
) -> Twhile
(f i
)
496 | Treturn
(i
) -> Treturn
(f i
)
497 | Tgoto
(i
) -> Tgoto
(f i
)
498 | Tdefault
(i
) -> Tdefault
(f i
)
499 | Tsizeof
(i
) -> Tsizeof
(f i
)
500 | Tasm
(i
) -> Tasm
(f i
)
501 | Tattribute
(i
) -> Tattribute
(f i
)
502 | Tinline
(i
) -> Tinline
(f i
)
503 | Ttypeof
(i
) -> Ttypeof
(f i
)
504 | EOF
(i
) -> EOF
(f i
)
507 (*****************************************************************************)
509 (*****************************************************************************)
511 let linecol_of_tok tok
=
512 let info = info_of_tok tok
in
513 Ast_c.line_of_info
info, Ast_c.col_of_info
info
515 let col_of_tok x
= snd
(linecol_of_tok x
)
516 let line_of_tok x
= fst
(linecol_of_tok x
)
517 let pos_of_tok x
= Ast_c.opos_of_info
(info_of_tok x
)
518 let str_of_tok x
= Ast_c.str_of_info
(info_of_tok x
)
519 let file_of_tok x
= Ast_c.file_of_info
(info_of_tok x
)
520 let pinfo_of_tok x
= Ast_c.pinfo_of_info
(info_of_tok x
)
523 match pinfo_of_tok x
with Ast_c.OriginTok _
-> true | _
-> false
525 match pinfo_of_tok x
with Ast_c.ExpandedTok _
-> true | _
-> false
527 match pinfo_of_tok x
with Ast_c.FakeTok _
-> true | _
-> false
529 match pinfo_of_tok x
with Ast_c.AbstractLineTok _
-> true | _
-> false
531 (*****************************************************************************)
533 (*****************************************************************************)
534 let is_same_line_or_close line tok
=
535 line_of_tok tok
=|= line
||
536 line_of_tok tok
=|= line
- 1 ||
537 line_of_tok tok
=|= line
- 2