3 * Copyright (C) 2010, University of Copenhagen DIKU and INRIA.
4 * Copyright (C) 2007, 2008 Ecole des Mines de Nantes
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License (GPL)
8 * version 2 as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * file license.txt for more details.
21 (*****************************************************************************)
22 (* Is_xxx, categories *)
23 (*****************************************************************************)
25 (* could define a type token_class = Comment | Ident | Operator | ...
26 * update: now token_c can maybe do that.
27 * but still, sometimes tokens belon to multiple classes. Could maybe
28 * return then a set of classes.
31 let is_space = function
32 | TCommentSpace _
-> true
33 | TCommentNewline _
-> true
36 let is_whitespace = is_space
38 let is_just_comment_or_space = function
40 | TCommentSpace _
-> true
41 | TCommentNewline _
-> true
43 let is_real_comment = is_just_comment_or_space
45 let is_just_comment = function
52 let is_comment = function
54 | TCommentSpace _
| TCommentNewline _
56 | TCommentMisc _
-> true
59 (* coupling with comment_annotater_c.ml.
60 * In fact more tokens than comments are not in the ast, but
61 * they were usually temporally created by ocamllex and removed
64 let is_not_in_ast = is_comment
66 let is_fake_comment = function
67 | TCommentCpp _
| TCommentMisc _
71 let is_not_comment x
=
75 (* ---------------------------------------------------------------------- *)
77 let is_cpp_instruction = function
80 | TIfdef _
| TIfdefelse _
| TIfdefelif _
| TEndif _
81 | TIfdefBool _
| TIfdefMisc _
| TIfdefVersion _
83 | TCppDirectiveOther _
88 let is_gcc_token = function
99 (* ---------------------------------------------------------------------- *)
100 let is_opar = function
101 | TOPar _
| TOParDefine _
-> true
104 let is_cpar = function
105 | TCPar _
| TCParEOL _
-> true
109 let is_obrace = function
110 | TOBrace _
| TOBraceDefineInit _
-> true
113 let is_cbrace = function
120 (* ---------------------------------------------------------------------- *)
123 let is_eof = function
129 let is_eom = function
133 let is_statement = function
134 | Tfor _
| Tdo _
| Tif _
| Twhile _
| Treturn _
135 | Tbreak _
| Telse _
| Tswitch _
| Tcase _
| Tcontinue _
142 (* is_start_of_something is used in parse_c for error recovery, to find
143 * a synchronisation token.
145 * Would like to put TIdent or TDefine, TIfdef but they can be in the
146 * middle of a function, for instance with label:.
148 * Could put Typedefident but fired ? it would work in error recovery
149 * on the already_passed tokens, which has been already gone in the
150 * Parsing_hacks.lookahead machinery, but it will not work on the
151 * "next" tokens. But because the namespace for labels is different
152 * from namespace for ident/typedef, we can use the name for a typedef
153 * for a label and so dangerous to put Typedefident at true here.
155 * Can look in parser_c.output to know what can be at toplevel
156 * at the very beginning.
159 let is_start_of_something = function
160 | Tchar _
| Tshort _
| Tint _
| Tdouble _
| Tfloat _
| Tlong _
161 | Tunsigned _
| Tsigned _
| Tvoid _
| Tsize_t _
| Tssize_t _
| Tptrdiff_t _
162 | Tauto _
| Tregister _
| Textern _
| Tstatic _
163 | Tconst _
| Tvolatile _
165 | Tstruct _
| Tunion _
| Tenum _
171 let is_binary_operator = function
172 | TOrLog _
| TAndLog _
| TOr _
| TXor _
| TAnd _
173 | TEqEq _
| TNotEq _
| TInf _
| TSup _
| TInfEq _
| TSupEq _
175 | TPlus _
| TMinus _
| TMul _
| TDiv _
| TMod _
179 let is_stuff_taking_parenthized = function
189 (* used in the algorithms for "10 most problematic errors" *)
190 let is_ident_like = function
194 | TDefParamVariadic _
199 | TMacroAttrStorage _
210 (*****************************************************************************)
212 (*****************************************************************************)
214 (* Because ocamlyacc force us to do it that way. The ocamlyacc token
215 * cant be a pair of a sum type, it must be directly a sum type.
217 let info_of_tok = function
218 | TString
((string, isWchar
), i
) -> i
219 | TChar
((string, isWchar
), i
) -> i
220 | TFloat
((string, floatType
), i
) -> i
222 | TAssign
(assignOp
, i
) -> i
225 | Tconstructorname
(s
, i
) -> i
226 | TypedefIdent
(s
, i
) -> i
231 | TInclude
(includes
, filename
, inifdef
, i1
) -> i1
234 | TCppDirectiveOther
(ii
) -> ii
236 | TIncludeStart
(i1
, inifdef
) -> i1
237 | TIncludeFilename
(s
, i1
) -> i1
240 | TOParDefine
(i1
) -> i1
241 | TIdentDefine
(s
, i
) -> i
242 | TCppEscapedNewline
(ii
) -> ii
243 | TDefParamVariadic
(s
, i1
) -> i1
245 | TCppConcatOp
(ii
) -> ii
247 | TOBraceDefineInit
(i1
) -> i1
251 | TMacroIdentBuilder
(s
, i
) -> i
252 | TMacroAttr
(s
, i
) -> i
253 | TMacroAttrStorage
(s
, i
) -> i
254 | TMacroStmt
(s
, i
) -> i
255 | TMacroString
(s
, i
) -> i
256 | TMacroDecl
(s
, i
) -> i
257 | TMacroDeclConst
(i
) -> i
258 | TMacroIterator
(s
,i
) -> i
259 (* | TMacroTop (s,i) -> i *)
260 | TCParEOL
(i1
) -> i1
265 | TCommentSpace
(i
) -> i
266 | TCommentNewline
(i
) -> i
267 | TCommentCpp
(cppkind
, i
) -> i
268 | TCommentMisc
(i
) -> i
270 | TCommentSkipTagStart
(i
) -> i
271 | TCommentSkipTagEnd
(i
) -> i
274 | TIfdefelse
(_
, i
) -> i
275 | TIfdefelif
(_
, i
) -> i
277 | TIfdefBool
(b
, _
, i
) -> i
278 | TIfdefMisc
(b
, _
, i
) -> i
279 | TIfdefVersion
(b
, _
, i
) -> i
329 | Tptrdiff_t
(i
) -> i
357 | Tattribute
(i
) -> i
358 | TattributeNoarg
(i
) -> i
368 (* used by tokens to complete the parse_info with filename, line, col infos *)
369 let visitor_info_of_tok f
= function
370 | TString
((s
, isWchar
), i
) -> TString
((s
, isWchar
), f i
)
371 | TChar
((s
, isWchar
), i
) -> TChar
((s
, isWchar
), f i
)
372 | TFloat
((s
, floatType
), i
) -> TFloat
((s
, floatType
), f i
)
373 | TAssign
(assignOp
, i
) -> TAssign
(assignOp
, f i
)
375 | TIdent
(s
, i
) -> TIdent
(s
, f i
)
376 | Tconstructorname
(s
, i
) -> Tconstructorname
(s
, f i
)
377 | TypedefIdent
(s
, i
) -> TypedefIdent
(s
, f i
)
378 | TInt
(s
, i
) -> TInt
(s
, f i
)
380 | TDefine
(i1
) -> TDefine
(f i1
)
382 | TUndef
(i1
) -> TUndef
(f i1
)
383 | TCppDirectiveOther
(i1
) -> TCppDirectiveOther
(f i1
)
385 | TInclude
(includes
, filename
, inifdef
, i1
) ->
386 TInclude
(includes
, filename
, inifdef
, f i1
)
388 | TIncludeStart
(i1
, inifdef
) -> TIncludeStart
(f i1
, inifdef
)
389 | TIncludeFilename
(s
, i1
) -> TIncludeFilename
(s
, f i1
)
391 | TCppEscapedNewline
(i1
) -> TCppEscapedNewline
(f i1
)
392 | TDefEOL
(i1
) -> TDefEOL
(f i1
)
394 | TCppConcatOp
(ii
) -> TCppConcatOp
(f ii
)
396 | TOParDefine
(i1
) -> TOParDefine
(f i1
)
397 | TIdentDefine
(s
, i
) -> TIdentDefine
(s
, f i
)
399 | TDefParamVariadic
(s
, i1
) -> TDefParamVariadic
(s
, f i1
)
401 | TOBraceDefineInit
(i1
) -> TOBraceDefineInit
(f i1
)
404 | TUnknown
(i
) -> TUnknown
(f i
)
406 | TMacroIdentBuilder
(s
, i
) -> TMacroIdentBuilder
(s
, f i
)
407 | TMacroAttr
(s
, i
) -> TMacroAttr
(s
, f i
)
408 | TMacroAttrStorage
(s
, i
) -> TMacroAttrStorage
(s
, f i
)
409 | TMacroStmt
(s
, i
) -> TMacroStmt
(s
, f i
)
410 | TMacroString
(s
, i
) -> TMacroString
(s
, f i
)
411 | TMacroDecl
(s
, i
) -> TMacroDecl
(s
, f i
)
412 | TMacroDeclConst
(i
) -> TMacroDeclConst
(f i
)
413 | TMacroIterator
(s
, i
) -> TMacroIterator
(s
, f i
)
414 (* | TMacroTop (s,i) -> TMacroTop (s,f i) *)
415 | TCParEOL
(i
) -> TCParEOL
(f i
)
418 | TAction
(i
) -> TAction
(f i
)
420 | TComment
(i
) -> TComment
(f i
)
421 | TCommentSpace
(i
) -> TCommentSpace
(f i
)
422 | TCommentNewline
(i
) -> TCommentNewline
(f i
)
423 | TCommentCpp
(cppkind
, i
) -> TCommentCpp
(cppkind
, f i
)
424 | TCommentMisc
(i
) -> TCommentMisc
(f i
)
426 | TCommentSkipTagStart
(i
) -> TCommentSkipTagStart
(f i
)
427 | TCommentSkipTagEnd
(i
) -> TCommentSkipTagEnd
(f i
)
429 | TIfdef
(t
, i
) -> TIfdef
(t
, f i
)
430 | TIfdefelse
(t
, i
) -> TIfdefelse
(t
, f i
)
431 | TIfdefelif
(t
, i
) -> TIfdefelif
(t
, f i
)
432 | TEndif
(t
, i
) -> TEndif
(t
, f i
)
433 | TIfdefBool
(b
, t
, i
) -> TIfdefBool
(b
, t
, f i
)
434 | TIfdefMisc
(b
, t
, i
) -> TIfdefMisc
(b
, t
, f i
)
435 | TIfdefVersion
(b
, t
, i
) -> TIfdefVersion
(b
, t
, f i
)
437 | TOPar
(i
) -> TOPar
(f i
)
438 | TCPar
(i
) -> TCPar
(f i
)
439 | TOBrace
(i
) -> TOBrace
(f i
)
440 | TCBrace
(i
) -> TCBrace
(f i
)
441 | TOCro
(i
) -> TOCro
(f i
)
442 | TCCro
(i
) -> TCCro
(f i
)
443 | TDot
(i
) -> TDot
(f i
)
444 | TComma
(i
) -> TComma
(f i
)
445 | TPtrOp
(i
) -> TPtrOp
(f i
)
446 | TInc
(i
) -> TInc
(f i
)
447 | TDec
(i
) -> TDec
(f i
)
448 | TEq
(i
) -> TEq
(f i
)
449 | TWhy
(i
) -> TWhy
(f i
)
450 | TTilde
(i
) -> TTilde
(f i
)
451 | TBang
(i
) -> TBang
(f i
)
452 | TEllipsis
(i
) -> TEllipsis
(f i
)
453 | TDotDot
(i
) -> TDotDot
(f i
)
454 | TPtVirg
(i
) -> TPtVirg
(f i
)
455 | TOrLog
(i
) -> TOrLog
(f i
)
456 | TAndLog
(i
) -> TAndLog
(f i
)
457 | TOr
(i
) -> TOr
(f i
)
458 | TXor
(i
) -> TXor
(f i
)
459 | TAnd
(i
) -> TAnd
(f i
)
460 | TEqEq
(i
) -> TEqEq
(f i
)
461 | TNotEq
(i
) -> TNotEq
(f i
)
462 | TInf
(i
) -> TInf
(f i
)
463 | TSup
(i
) -> TSup
(f i
)
464 | TInfEq
(i
) -> TInfEq
(f i
)
465 | TSupEq
(i
) -> TSupEq
(f i
)
466 | TShl
(i
) -> TShl
(f i
)
467 | TShr
(i
) -> TShr
(f i
)
468 | TPlus
(i
) -> TPlus
(f i
)
469 | TMinus
(i
) -> TMinus
(f i
)
470 | TMul
(i
) -> TMul
(f i
)
471 | TDiv
(i
) -> TDiv
(f i
)
472 | TMod
(i
) -> TMod
(f i
)
473 | Tchar
(i
) -> Tchar
(f i
)
474 | Tshort
(i
) -> Tshort
(f i
)
475 | Tint
(i
) -> Tint
(f i
)
476 | Tdouble
(i
) -> Tdouble
(f i
)
477 | Tfloat
(i
) -> Tfloat
(f i
)
478 | Tlong
(i
) -> Tlong
(f i
)
479 | Tunsigned
(i
) -> Tunsigned
(f i
)
480 | Tsigned
(i
) -> Tsigned
(f i
)
481 | Tvoid
(i
) -> Tvoid
(f i
)
482 | Tsize_t
(i
) -> Tsize_t
(f i
)
483 | Tssize_t
(i
) -> Tssize_t
(f i
)
484 | Tptrdiff_t
(i
) -> Tptrdiff_t
(f i
)
485 | Tauto
(i
) -> Tauto
(f i
)
486 | Tregister
(i
) -> Tregister
(f i
)
487 | Textern
(i
) -> Textern
(f i
)
488 | Tstatic
(i
) -> Tstatic
(f i
)
489 | Tconst
(i
) -> Tconst
(f i
)
490 | Tvolatile
(i
) -> Tvolatile
(f i
)
492 | Trestrict
(i
) -> Trestrict
(f i
)
494 | Tstruct
(i
) -> Tstruct
(f i
)
495 | Tenum
(i
) -> Tenum
(f i
)
496 | Ttypedef
(i
) -> Ttypedef
(f i
)
497 | Tunion
(i
) -> Tunion
(f i
)
498 | Tbreak
(i
) -> Tbreak
(f i
)
499 | Telse
(i
) -> Telse
(f i
)
500 | Tswitch
(i
) -> Tswitch
(f i
)
501 | Tcase
(i
) -> Tcase
(f i
)
502 | Tcontinue
(i
) -> Tcontinue
(f i
)
503 | Tfor
(i
) -> Tfor
(f i
)
504 | Tdo
(i
) -> Tdo
(f i
)
505 | Tif
(i
) -> Tif
(f i
)
506 | Twhile
(i
) -> Twhile
(f i
)
507 | Treturn
(i
) -> Treturn
(f i
)
508 | Tgoto
(i
) -> Tgoto
(f i
)
509 | Tdefault
(i
) -> Tdefault
(f i
)
510 | Tsizeof
(i
) -> Tsizeof
(f i
)
511 | Tasm
(i
) -> Tasm
(f i
)
512 | Tattribute
(i
) -> Tattribute
(f i
)
513 | TattributeNoarg
(i
) -> TattributeNoarg
(f i
)
514 | Tinline
(i
) -> Tinline
(f i
)
515 | Ttypeof
(i
) -> Ttypeof
(f i
)
516 | Tnew
(i
) -> Tnew
(f i
)
517 | EOF
(i
) -> EOF
(f i
)
520 (*****************************************************************************)
522 (*****************************************************************************)
524 let linecol_of_tok tok
=
525 let info = info_of_tok tok
in
526 Ast_c.line_of_info
info, Ast_c.col_of_info
info
528 let col_of_tok x
= snd
(linecol_of_tok x
)
529 let line_of_tok x
= fst
(linecol_of_tok x
)
530 let pos_of_tok x
= Ast_c.opos_of_info
(info_of_tok x
)
531 let str_of_tok x
= Ast_c.str_of_info
(info_of_tok x
)
532 let file_of_tok x
= Ast_c.file_of_info
(info_of_tok x
)
533 let pinfo_of_tok x
= Ast_c.pinfo_of_info
(info_of_tok x
)
536 match pinfo_of_tok x
with Ast_c.OriginTok _
-> true | _
-> false
538 match pinfo_of_tok x
with Ast_c.ExpandedTok _
-> true | _
-> false
540 match pinfo_of_tok x
with Ast_c.FakeTok _
-> true | _
-> false
542 match pinfo_of_tok x
with Ast_c.AbstractLineTok _
-> true | _
-> false
544 (*****************************************************************************)
546 (*****************************************************************************)
547 let is_same_line_or_close line tok
=
548 line_of_tok tok
=|= line
||
549 line_of_tok tok
=|= line
- 1 ||
550 line_of_tok tok
=|= line
- 2