cefa5e9fcd9395b1d768e2764200afe1225c3e8f
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 (* ---------------------------------------------------------------------- *)
120 let is_eof = function
126 let is_statement = function
127 | Tfor _
| Tdo _
| Tif _
| Twhile _
| Treturn _
128 | Tbreak _
| Telse _
| Tswitch _
| Tcase _
| Tcontinue _
135 (* is_start_of_something is used in parse_c for error recovery, to find
136 * a synchronisation token.
138 * Would like to put TIdent or TDefine, TIfdef but they can be in the
139 * middle of a function, for instance with label:.
141 * Could put Typedefident but fired ? it would work in error recovery
142 * on the already_passed tokens, which has been already gone in the
143 * Parsing_hacks.lookahead machinery, but it will not work on the
144 * "next" tokens. But because the namespace for labels is different
145 * from namespace for ident/typedef, we can use the name for a typedef
146 * for a label and so dangerous to put Typedefident at true here.
148 * Can look in parser_c.output to know what can be at toplevel
149 * at the very beginning.
152 let is_start_of_something = function
153 | Tchar _
| Tshort _
| Tint _
| Tdouble _
| Tfloat _
| Tlong _
154 | Tunsigned _
| Tsigned _
| Tvoid _
155 | Tauto _
| Tregister _
| Textern _
| Tstatic _
156 | Tconst _
| Tvolatile _
158 | Tstruct _
| Tunion _
| Tenum _
164 let is_binary_operator = function
165 | TOrLog _
| TAndLog _
| TOr _
| TXor _
| TAnd _
166 | TEqEq _
| TNotEq _
| TInf _
| TSup _
| TInfEq _
| TSupEq _
168 | TPlus _
| TMinus _
| TMul _
| TDiv _
| TMod _
172 let is_stuff_taking_parenthized = function
182 (* used in the algorithms for "10 most problematic errors" *)
183 let is_ident_like = function
187 | TDefParamVariadic _
192 | TMacroAttrStorage _
204 (*****************************************************************************)
206 (*****************************************************************************)
208 (* Because ocamlyacc force us to do it that way. The ocamlyacc token
209 * cant be a pair of a sum type, it must be directly a sum type.
211 let info_of_tok = function
212 | TString
((string, isWchar
), i
) -> i
213 | TChar
((string, isWchar
), i
) -> i
214 | TFloat
((string, floatType
), i
) -> i
216 | TAssign
(assignOp
, i
) -> i
219 | TypedefIdent
(s
, i
) -> i
224 | TInclude
(includes
, filename
, inifdef
, i1
) -> i1
226 | TUndef
(s
, ii
) -> ii
227 | TCppDirectiveOther
(ii
) -> ii
229 | TIncludeStart
(i1
, inifdef
) -> i1
230 | TIncludeFilename
(s
, i1
) -> i1
233 | TOParDefine
(i1
) -> i1
234 | TIdentDefine
(s
, i
) -> i
235 | TCppEscapedNewline
(ii
) -> ii
236 | TDefParamVariadic
(s
, i1
) -> i1
238 | TCppConcatOp
(ii
) -> ii
240 | TOBraceDefineInit
(i1
) -> i1
244 | TMacroIdentBuilder
(s
, i
) -> i
245 | TMacroAttr
(s
, i
) -> i
246 | TMacroAttrStorage
(s
, i
) -> i
247 | TMacroStmt
(s
, i
) -> i
248 | TMacroString
(s
, i
) -> i
249 | TMacroDecl
(s
, i
) -> i
250 | TMacroStructDecl
(s
, i
) -> i
251 | TMacroDeclConst
(i
) -> i
252 | TMacroIterator
(s
,i
) -> i
253 (* | TMacroTop (s,i) -> i *)
254 | TCParEOL
(i1
) -> i1
259 | TCommentSpace
(i
) -> i
260 | TCommentNewline
(i
) -> i
261 | TCommentCpp
(cppkind
, i
) -> i
262 | TCommentMisc
(i
) -> i
264 | TCommentSkipTagStart
(i
) -> i
265 | TCommentSkipTagEnd
(i
) -> i
268 | TIfdefelse
(_
, i
) -> i
269 | TIfdefelif
(_
, i
) -> i
271 | TIfdefBool
(b
, _
, i
) -> i
272 | TIfdefMisc
(b
, _
, i
) -> i
273 | TIfdefVersion
(b
, _
, i
) -> i
348 | Tattribute
(i
) -> i
357 (* used by tokens to complete the parse_info with filename, line, col infos *)
358 let visitor_info_of_tok f
= function
359 | TString
((s
, isWchar
), i
) -> TString
((s
, isWchar
), f i
)
360 | TChar
((s
, isWchar
), i
) -> TChar
((s
, isWchar
), f i
)
361 | TFloat
((s
, floatType
), i
) -> TFloat
((s
, floatType
), f i
)
362 | TAssign
(assignOp
, i
) -> TAssign
(assignOp
, f i
)
364 | TIdent
(s
, i
) -> TIdent
(s
, f i
)
365 | TypedefIdent
(s
, i
) -> TypedefIdent
(s
, f i
)
366 | TInt
(s
, i
) -> TInt
(s
, f i
)
368 | TDefine
(i1
) -> TDefine
(f i1
)
370 | TUndef
(s
,i1
) -> TUndef
(s
, f i1
)
371 | TCppDirectiveOther
(i1
) -> TCppDirectiveOther
(f i1
)
373 | TInclude
(includes
, filename
, inifdef
, i1
) ->
374 TInclude
(includes
, filename
, inifdef
, f i1
)
376 | TIncludeStart
(i1
, inifdef
) -> TIncludeStart
(f i1
, inifdef
)
377 | TIncludeFilename
(s
, i1
) -> TIncludeFilename
(s
, f i1
)
379 | TCppEscapedNewline
(i1
) -> TCppEscapedNewline
(f i1
)
380 | TDefEOL
(i1
) -> TDefEOL
(f i1
)
382 | TCppConcatOp
(ii
) -> TCppConcatOp
(f ii
)
384 | TOParDefine
(i1
) -> TOParDefine
(f i1
)
385 | TIdentDefine
(s
, i
) -> TIdentDefine
(s
, f i
)
387 | TDefParamVariadic
(s
, i1
) -> TDefParamVariadic
(s
, f i1
)
389 | TOBraceDefineInit
(i1
) -> TOBraceDefineInit
(f i1
)
392 | TUnknown
(i
) -> TUnknown
(f i
)
394 | TMacroIdentBuilder
(s
, i
) -> TMacroIdentBuilder
(s
, f i
)
395 | TMacroAttr
(s
, i
) -> TMacroAttr
(s
, f i
)
396 | TMacroAttrStorage
(s
, i
) -> TMacroAttrStorage
(s
, f i
)
397 | TMacroStmt
(s
, i
) -> TMacroStmt
(s
, f i
)
398 | TMacroString
(s
, i
) -> TMacroString
(s
, f i
)
399 | TMacroDecl
(s
, i
) -> TMacroDecl
(s
, f i
)
400 | TMacroStructDecl
(s
, i
) -> TMacroStructDecl
(s
, f i
)
401 | TMacroDeclConst
(i
) -> TMacroDeclConst
(f i
)
402 | TMacroIterator
(s
, i
) -> TMacroIterator
(s
, f i
)
403 (* | TMacroTop (s,i) -> TMacroTop (s,f i) *)
404 | TCParEOL
(i
) -> TCParEOL
(f i
)
407 | TAction
(i
) -> TAction
(f i
)
409 | TComment
(i
) -> TComment
(f i
)
410 | TCommentSpace
(i
) -> TCommentSpace
(f i
)
411 | TCommentNewline
(i
) -> TCommentNewline
(f i
)
412 | TCommentCpp
(cppkind
, i
) -> TCommentCpp
(cppkind
, f i
)
413 | TCommentMisc
(i
) -> TCommentMisc
(f i
)
415 | TCommentSkipTagStart
(i
) -> TCommentSkipTagStart
(f i
)
416 | TCommentSkipTagEnd
(i
) -> TCommentSkipTagEnd
(f i
)
418 | TIfdef
(t
, i
) -> TIfdef
(t
, f i
)
419 | TIfdefelse
(t
, i
) -> TIfdefelse
(t
, f i
)
420 | TIfdefelif
(t
, i
) -> TIfdefelif
(t
, f i
)
421 | TEndif
(t
, i
) -> TEndif
(t
, f i
)
422 | TIfdefBool
(b
, t
, i
) -> TIfdefBool
(b
, t
, f i
)
423 | TIfdefMisc
(b
, t
, i
) -> TIfdefMisc
(b
, t
, f i
)
424 | TIfdefVersion
(b
, t
, i
) -> TIfdefVersion
(b
, t
, f i
)
426 | TOPar
(i
) -> TOPar
(f i
)
427 | TCPar
(i
) -> TCPar
(f i
)
428 | TOBrace
(i
) -> TOBrace
(f i
)
429 | TCBrace
(i
) -> TCBrace
(f i
)
430 | TOCro
(i
) -> TOCro
(f i
)
431 | TCCro
(i
) -> TCCro
(f i
)
432 | TDot
(i
) -> TDot
(f i
)
433 | TComma
(i
) -> TComma
(f i
)
434 | TPtrOp
(i
) -> TPtrOp
(f i
)
435 | TInc
(i
) -> TInc
(f i
)
436 | TDec
(i
) -> TDec
(f i
)
437 | TEq
(i
) -> TEq
(f i
)
438 | TWhy
(i
) -> TWhy
(f i
)
439 | TTilde
(i
) -> TTilde
(f i
)
440 | TBang
(i
) -> TBang
(f i
)
441 | TEllipsis
(i
) -> TEllipsis
(f i
)
442 | TDotDot
(i
) -> TDotDot
(f i
)
443 | TPtVirg
(i
) -> TPtVirg
(f i
)
444 | TOrLog
(i
) -> TOrLog
(f i
)
445 | TAndLog
(i
) -> TAndLog
(f i
)
446 | TOr
(i
) -> TOr
(f i
)
447 | TXor
(i
) -> TXor
(f i
)
448 | TAnd
(i
) -> TAnd
(f i
)
449 | TEqEq
(i
) -> TEqEq
(f i
)
450 | TNotEq
(i
) -> TNotEq
(f i
)
451 | TInf
(i
) -> TInf
(f i
)
452 | TSup
(i
) -> TSup
(f i
)
453 | TInfEq
(i
) -> TInfEq
(f i
)
454 | TSupEq
(i
) -> TSupEq
(f i
)
455 | TShl
(i
) -> TShl
(f i
)
456 | TShr
(i
) -> TShr
(f i
)
457 | TPlus
(i
) -> TPlus
(f i
)
458 | TMinus
(i
) -> TMinus
(f i
)
459 | TMul
(i
) -> TMul
(f i
)
460 | TDiv
(i
) -> TDiv
(f i
)
461 | TMod
(i
) -> TMod
(f i
)
462 | Tchar
(i
) -> Tchar
(f i
)
463 | Tshort
(i
) -> Tshort
(f i
)
464 | Tint
(i
) -> Tint
(f i
)
465 | Tdouble
(i
) -> Tdouble
(f i
)
466 | Tfloat
(i
) -> Tfloat
(f i
)
467 | Tlong
(i
) -> Tlong
(f i
)
468 | Tunsigned
(i
) -> Tunsigned
(f i
)
469 | Tsigned
(i
) -> Tsigned
(f i
)
470 | Tvoid
(i
) -> Tvoid
(f i
)
471 | Tauto
(i
) -> Tauto
(f i
)
472 | Tregister
(i
) -> Tregister
(f i
)
473 | Textern
(i
) -> Textern
(f i
)
474 | Tstatic
(i
) -> Tstatic
(f i
)
475 | Tconst
(i
) -> Tconst
(f i
)
476 | Tvolatile
(i
) -> Tvolatile
(f i
)
478 | Trestrict
(i
) -> Trestrict
(f i
)
480 | Tstruct
(i
) -> Tstruct
(f i
)
481 | Tenum
(i
) -> Tenum
(f i
)
482 | Ttypedef
(i
) -> Ttypedef
(f i
)
483 | Tunion
(i
) -> Tunion
(f i
)
484 | Tbreak
(i
) -> Tbreak
(f i
)
485 | Telse
(i
) -> Telse
(f i
)
486 | Tswitch
(i
) -> Tswitch
(f i
)
487 | Tcase
(i
) -> Tcase
(f i
)
488 | Tcontinue
(i
) -> Tcontinue
(f i
)
489 | Tfor
(i
) -> Tfor
(f i
)
490 | Tdo
(i
) -> Tdo
(f i
)
491 | Tif
(i
) -> Tif
(f i
)
492 | Twhile
(i
) -> Twhile
(f i
)
493 | Treturn
(i
) -> Treturn
(f i
)
494 | Tgoto
(i
) -> Tgoto
(f i
)
495 | Tdefault
(i
) -> Tdefault
(f i
)
496 | Tsizeof
(i
) -> Tsizeof
(f i
)
497 | Tasm
(i
) -> Tasm
(f i
)
498 | Tattribute
(i
) -> Tattribute
(f i
)
499 | Tinline
(i
) -> Tinline
(f i
)
500 | Ttypeof
(i
) -> Ttypeof
(f i
)
501 | EOF
(i
) -> EOF
(f i
)
504 (*****************************************************************************)
506 (*****************************************************************************)
508 let linecol_of_tok tok
=
509 let info = info_of_tok tok
in
510 Ast_c.line_of_info
info, Ast_c.col_of_info
info
512 let col_of_tok x
= snd
(linecol_of_tok x
)
513 let line_of_tok x
= fst
(linecol_of_tok x
)
514 let pos_of_tok x
= Ast_c.opos_of_info
(info_of_tok x
)
515 let str_of_tok x
= Ast_c.str_of_info
(info_of_tok x
)
516 let file_of_tok x
= Ast_c.file_of_info
(info_of_tok x
)
517 let pinfo_of_tok x
= Ast_c.pinfo_of_info
(info_of_tok x
)
520 match pinfo_of_tok x
with Ast_c.OriginTok _
-> true | _
-> false
522 match pinfo_of_tok x
with Ast_c.ExpandedTok _
-> true | _
-> false
524 match pinfo_of_tok x
with Ast_c.FakeTok _
-> true | _
-> false
526 match pinfo_of_tok x
with Ast_c.AbstractLineTok _
-> true | _
-> false
528 (*****************************************************************************)
530 (*****************************************************************************)
531 let is_same_line_or_close line tok
=
532 line_of_tok tok
=|= line
||
533 line_of_tok tok
=|= line
- 1 ||
534 line_of_tok tok
=|= line
- 2