0a3e2e4a89ec3309e17353cd0b3e1204e011c54f
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 _
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 | TypedefIdent
(s
, i
) -> i
230 | TInclude
(includes
, filename
, inifdef
, i1
) -> i1
232 | TUndef
(s
, ii
) -> ii
233 | TCppDirectiveOther
(ii
) -> ii
235 | TIncludeStart
(i1
, inifdef
) -> i1
236 | TIncludeFilename
(s
, i1
) -> i1
239 | TOParDefine
(i1
) -> i1
240 | TIdentDefine
(s
, i
) -> i
241 | TCppEscapedNewline
(ii
) -> ii
242 | TDefParamVariadic
(s
, i1
) -> i1
244 | TCppConcatOp
(ii
) -> ii
246 | TOBraceDefineInit
(i1
) -> i1
250 | TMacroIdentBuilder
(s
, i
) -> i
251 | TMacroAttr
(s
, i
) -> i
252 | TMacroAttrStorage
(s
, i
) -> i
253 | TMacroStmt
(s
, i
) -> i
254 | TMacroString
(s
, i
) -> i
255 | TMacroDecl
(s
, i
) -> i
256 | TMacroDeclConst
(i
) -> i
257 | TMacroIterator
(s
,i
) -> i
258 (* | TMacroTop (s,i) -> i *)
259 | TCParEOL
(i1
) -> i1
264 | TCommentSpace
(i
) -> i
265 | TCommentNewline
(i
) -> i
266 | TCommentCpp
(cppkind
, i
) -> i
267 | TCommentMisc
(i
) -> i
269 | TCommentSkipTagStart
(i
) -> i
270 | TCommentSkipTagEnd
(i
) -> i
273 | TIfdefelse
(_
, i
) -> i
274 | TIfdefelif
(_
, i
) -> i
276 | TIfdefBool
(b
, _
, i
) -> i
277 | TIfdefMisc
(b
, _
, i
) -> i
278 | TIfdefVersion
(b
, _
, i
) -> i
353 | Tattribute
(i
) -> i
354 | TattributeNoarg
(i
) -> i
363 (* used by tokens to complete the parse_info with filename, line, col infos *)
364 let visitor_info_of_tok f
= function
365 | TString
((s
, isWchar
), i
) -> TString
((s
, isWchar
), f i
)
366 | TChar
((s
, isWchar
), i
) -> TChar
((s
, isWchar
), f i
)
367 | TFloat
((s
, floatType
), i
) -> TFloat
((s
, floatType
), f i
)
368 | TAssign
(assignOp
, i
) -> TAssign
(assignOp
, f i
)
370 | TIdent
(s
, i
) -> TIdent
(s
, f i
)
371 | TypedefIdent
(s
, i
) -> TypedefIdent
(s
, f i
)
372 | TInt
(s
, i
) -> TInt
(s
, f i
)
374 | TDefine
(i1
) -> TDefine
(f i1
)
376 | TUndef
(s
,i1
) -> TUndef
(s
, f i1
)
377 | TCppDirectiveOther
(i1
) -> TCppDirectiveOther
(f i1
)
379 | TInclude
(includes
, filename
, inifdef
, i1
) ->
380 TInclude
(includes
, filename
, inifdef
, f i1
)
382 | TIncludeStart
(i1
, inifdef
) -> TIncludeStart
(f i1
, inifdef
)
383 | TIncludeFilename
(s
, i1
) -> TIncludeFilename
(s
, f i1
)
385 | TCppEscapedNewline
(i1
) -> TCppEscapedNewline
(f i1
)
386 | TDefEOL
(i1
) -> TDefEOL
(f i1
)
388 | TCppConcatOp
(ii
) -> TCppConcatOp
(f ii
)
390 | TOParDefine
(i1
) -> TOParDefine
(f i1
)
391 | TIdentDefine
(s
, i
) -> TIdentDefine
(s
, f i
)
393 | TDefParamVariadic
(s
, i1
) -> TDefParamVariadic
(s
, f i1
)
395 | TOBraceDefineInit
(i1
) -> TOBraceDefineInit
(f i1
)
398 | TUnknown
(i
) -> TUnknown
(f i
)
400 | TMacroIdentBuilder
(s
, i
) -> TMacroIdentBuilder
(s
, f i
)
401 | TMacroAttr
(s
, i
) -> TMacroAttr
(s
, f i
)
402 | TMacroAttrStorage
(s
, i
) -> TMacroAttrStorage
(s
, f i
)
403 | TMacroStmt
(s
, i
) -> TMacroStmt
(s
, f i
)
404 | TMacroString
(s
, i
) -> TMacroString
(s
, f i
)
405 | TMacroDecl
(s
, i
) -> TMacroDecl
(s
, f i
)
406 | TMacroDeclConst
(i
) -> TMacroDeclConst
(f i
)
407 | TMacroIterator
(s
, i
) -> TMacroIterator
(s
, f i
)
408 (* | TMacroTop (s,i) -> TMacroTop (s,f i) *)
409 | TCParEOL
(i
) -> TCParEOL
(f i
)
412 | TAction
(i
) -> TAction
(f i
)
414 | TComment
(i
) -> TComment
(f i
)
415 | TCommentSpace
(i
) -> TCommentSpace
(f i
)
416 | TCommentNewline
(i
) -> TCommentNewline
(f i
)
417 | TCommentCpp
(cppkind
, i
) -> TCommentCpp
(cppkind
, f i
)
418 | TCommentMisc
(i
) -> TCommentMisc
(f i
)
420 | TCommentSkipTagStart
(i
) -> TCommentSkipTagStart
(f i
)
421 | TCommentSkipTagEnd
(i
) -> TCommentSkipTagEnd
(f i
)
423 | TIfdef
(t
, i
) -> TIfdef
(t
, f i
)
424 | TIfdefelse
(t
, i
) -> TIfdefelse
(t
, f i
)
425 | TIfdefelif
(t
, i
) -> TIfdefelif
(t
, f i
)
426 | TEndif
(t
, i
) -> TEndif
(t
, f i
)
427 | TIfdefBool
(b
, t
, i
) -> TIfdefBool
(b
, t
, f i
)
428 | TIfdefMisc
(b
, t
, i
) -> TIfdefMisc
(b
, t
, f i
)
429 | TIfdefVersion
(b
, t
, i
) -> TIfdefVersion
(b
, t
, f i
)
431 | TOPar
(i
) -> TOPar
(f i
)
432 | TCPar
(i
) -> TCPar
(f i
)
433 | TOBrace
(i
) -> TOBrace
(f i
)
434 | TCBrace
(i
) -> TCBrace
(f i
)
435 | TOCro
(i
) -> TOCro
(f i
)
436 | TCCro
(i
) -> TCCro
(f i
)
437 | TDot
(i
) -> TDot
(f i
)
438 | TComma
(i
) -> TComma
(f i
)
439 | TPtrOp
(i
) -> TPtrOp
(f i
)
440 | TInc
(i
) -> TInc
(f i
)
441 | TDec
(i
) -> TDec
(f i
)
442 | TEq
(i
) -> TEq
(f i
)
443 | TWhy
(i
) -> TWhy
(f i
)
444 | TTilde
(i
) -> TTilde
(f i
)
445 | TBang
(i
) -> TBang
(f i
)
446 | TEllipsis
(i
) -> TEllipsis
(f i
)
447 | TDotDot
(i
) -> TDotDot
(f i
)
448 | TPtVirg
(i
) -> TPtVirg
(f i
)
449 | TOrLog
(i
) -> TOrLog
(f i
)
450 | TAndLog
(i
) -> TAndLog
(f i
)
451 | TOr
(i
) -> TOr
(f i
)
452 | TXor
(i
) -> TXor
(f i
)
453 | TAnd
(i
) -> TAnd
(f i
)
454 | TEqEq
(i
) -> TEqEq
(f i
)
455 | TNotEq
(i
) -> TNotEq
(f i
)
456 | TInf
(i
) -> TInf
(f i
)
457 | TSup
(i
) -> TSup
(f i
)
458 | TInfEq
(i
) -> TInfEq
(f i
)
459 | TSupEq
(i
) -> TSupEq
(f i
)
460 | TShl
(i
) -> TShl
(f i
)
461 | TShr
(i
) -> TShr
(f i
)
462 | TPlus
(i
) -> TPlus
(f i
)
463 | TMinus
(i
) -> TMinus
(f i
)
464 | TMul
(i
) -> TMul
(f i
)
465 | TDiv
(i
) -> TDiv
(f i
)
466 | TMod
(i
) -> TMod
(f i
)
467 | Tchar
(i
) -> Tchar
(f i
)
468 | Tshort
(i
) -> Tshort
(f i
)
469 | Tint
(i
) -> Tint
(f i
)
470 | Tdouble
(i
) -> Tdouble
(f i
)
471 | Tfloat
(i
) -> Tfloat
(f i
)
472 | Tlong
(i
) -> Tlong
(f i
)
473 | Tunsigned
(i
) -> Tunsigned
(f i
)
474 | Tsigned
(i
) -> Tsigned
(f i
)
475 | Tvoid
(i
) -> Tvoid
(f i
)
476 | Tauto
(i
) -> Tauto
(f i
)
477 | Tregister
(i
) -> Tregister
(f i
)
478 | Textern
(i
) -> Textern
(f i
)
479 | Tstatic
(i
) -> Tstatic
(f i
)
480 | Tconst
(i
) -> Tconst
(f i
)
481 | Tvolatile
(i
) -> Tvolatile
(f i
)
483 | Trestrict
(i
) -> Trestrict
(f i
)
485 | Tstruct
(i
) -> Tstruct
(f i
)
486 | Tenum
(i
) -> Tenum
(f i
)
487 | Ttypedef
(i
) -> Ttypedef
(f i
)
488 | Tunion
(i
) -> Tunion
(f i
)
489 | Tbreak
(i
) -> Tbreak
(f i
)
490 | Telse
(i
) -> Telse
(f i
)
491 | Tswitch
(i
) -> Tswitch
(f i
)
492 | Tcase
(i
) -> Tcase
(f i
)
493 | Tcontinue
(i
) -> Tcontinue
(f i
)
494 | Tfor
(i
) -> Tfor
(f i
)
495 | Tdo
(i
) -> Tdo
(f i
)
496 | Tif
(i
) -> Tif
(f i
)
497 | Twhile
(i
) -> Twhile
(f i
)
498 | Treturn
(i
) -> Treturn
(f i
)
499 | Tgoto
(i
) -> Tgoto
(f i
)
500 | Tdefault
(i
) -> Tdefault
(f i
)
501 | Tsizeof
(i
) -> Tsizeof
(f i
)
502 | Tasm
(i
) -> Tasm
(f i
)
503 | Tattribute
(i
) -> Tattribute
(f i
)
504 | TattributeNoarg
(i
) -> TattributeNoarg
(f i
)
505 | Tinline
(i
) -> Tinline
(f i
)
506 | Ttypeof
(i
) -> Ttypeof
(f i
)
507 | EOF
(i
) -> EOF
(f i
)
510 (*****************************************************************************)
512 (*****************************************************************************)
514 let linecol_of_tok tok
=
515 let info = info_of_tok tok
in
516 Ast_c.line_of_info
info, Ast_c.col_of_info
info
518 let col_of_tok x
= snd
(linecol_of_tok x
)
519 let line_of_tok x
= fst
(linecol_of_tok x
)
520 let pos_of_tok x
= Ast_c.opos_of_info
(info_of_tok x
)
521 let str_of_tok x
= Ast_c.str_of_info
(info_of_tok x
)
522 let file_of_tok x
= Ast_c.file_of_info
(info_of_tok x
)
523 let pinfo_of_tok x
= Ast_c.pinfo_of_info
(info_of_tok x
)
526 match pinfo_of_tok x
with Ast_c.OriginTok _
-> true | _
-> false
528 match pinfo_of_tok x
with Ast_c.ExpandedTok _
-> true | _
-> false
530 match pinfo_of_tok x
with Ast_c.FakeTok _
-> true | _
-> false
532 match pinfo_of_tok x
with Ast_c.AbstractLineTok _
-> true | _
-> false
534 (*****************************************************************************)
536 (*****************************************************************************)
537 let is_same_line_or_close line tok
=
538 line_of_tok tok
=|= line
||
539 line_of_tok tok
=|= line
- 1 ||
540 line_of_tok tok
=|= line
- 2