FreeBASIC  0.91.0
parser-comment.bas
Go to the documentation of this file.
1 '' comments (REM or "'") and directives ("$") parsing
2 ''
3 '' chng: sep/2004 written [v1ctor]
4 
5 
6 #include once "fb.bi"
7 #include once "fbint.bi"
8 #include once "parser.bi"
9 
10 #define LEX_FLAGS (LEXCHECK_NOLINECONT or LEXCHECK_NODEFINE or LEXCHECK_NOSUFFIX or LEXCHECK_NOMULTILINECOMMENT)
11 
12 declare sub cDirective( )
13 
14 '':::::
15 ''Comment = (COMMENT_CHAR | REM) ((DIRECTIVE_CHAR Directive)
16 '' | (any_char_but_EOL*)) .
17 ''
18 function cComment _
19  ( _
20  byval lexflags as LEXCHECK _
21  ) as integer
22 
23  select case lexGetToken( lexflags )
24  case FB_TK_COMMENT, FB_TK_REM
25  '' Prevent the PP from trying to parse (pointless in a comment
26  '' anyways), and -pp from emitting the tokens (specifically due
27  '' to the lexSkipToken() calls for '$' and from cDirective(),
28  '' when parsing a $ meta command)
29  lex.ctx->reclevel += 1
30  lexSkipToken( LEX_FLAGS )
31 
32  if( lexGetToken( LEX_FLAGS ) = FB_TK_DIRECTIVECHAR ) then
33  lexSkipToken( LEX_FLAGS )
34  cDirective( )
35  else
36  lexSkipLine( )
37  end if
38 
39  lex.ctx->reclevel -= 1
40  function = TRUE
41 
42  case else
43  function = FALSE
44  end select
45 
46 end function
47 
48 '':::::
49 ''Directive = INCLUDE ONCE? ':' '\'' STR_LIT '\''
50 '' | DYNAMIC
51 '' | STATIC .
52 '' | LANG ':' '\"' STR_LIT '\"'
53 ''
54 sub cDirective( ) static
55  static as zstring * FB_MAXPATHLEN+1 incfile
56  dim as integer isonce
57 
58  select case as const lexGetToken( )
59  case FB_TK_DYNAMIC
60  if( fbLangOptIsSet( FB_LANG_OPT_METACMD ) = FALSE ) then
61  errReportNotAllowed( FB_LANG_OPT_METACMD )
62  else
63  lexSkipToken( )
64  env.opt.dynamic = TRUE
65 
66  '' Preserve under -pp
67  if( env.ppfile_num > 0 ) then
68  lexPPOnlyEmitText( "'$dynamic" )
69  end if
70  end if
71 
72 
73  case FB_TK_STATIC
74  if( fbLangOptIsSet( FB_LANG_OPT_METACMD ) = FALSE ) then
75  errReportNotAllowed( FB_LANG_OPT_METACMD )
76  else
77  lexSkipToken( )
78  env.opt.dynamic = FALSE
79 
80  '' Preserve under -pp
81  if( env.ppfile_num > 0 ) then
82  lexPPOnlyEmitText( "'$static" )
83  end if
84  end if
85 
86  case FB_TK_INCLUDE
87  if( fbLangOptIsSet( FB_LANG_OPT_METACMD ) = FALSE ) then
88  errReportNotAllowed( FB_LANG_OPT_METACMD )
89  else
90  lexSkipToken( )
91 
92  '' ONCE?
93  isonce = FALSE
94  if( hMatchText( "ONCE" ) ) then
95  isonce = TRUE
96  end if
97 
98  '' ':'
99  if( hMatch( FB_TK_STMTSEP ) = FALSE ) then
100  errReport( FB_ERRMSG_SYNTAXERROR )
101  exit select
102  end if
103 
104  '' "STR_LIT"
105  if( lexGetClass( ) = FB_TKCLASS_STRLITERAL ) then
106  lexEatToken( incfile )
107 
108  else
109  '' '\''
110  if( lexGetToken( LEX_FLAGS or LEXCHECK_NOWHITESPC ) <> FB_TK_COMMENT ) then
111  errReport( FB_ERRMSG_SYNTAXERROR )
112  exit select
113  else
114  lexSkipToken( LEX_FLAGS or LEXCHECK_NOWHITESPC )
115  end if
116 
117  lexReadLine( CHAR_APOST, @incfile )
118 
119  '' '\''
120  if( hMatch( CHAR_APOST ) = FALSE ) then
121  errReport( FB_ERRMSG_SYNTAXERROR )
122  exit select
123  end if
124  end if
125 
126  fbIncludeFile( incfile, isonce )
127  end if
128 
129  case else
130  select case lexGetClass( )
131  case FB_TKCLASS_KEYWORD, FB_TKCLASS_QUIRKWD
132  if( fbLangOptIsSet( FB_LANG_OPT_METACMD ) ) then
133  errReport( FB_ERRMSG_SYNTAXERROR )
134  end if
135 
136  case else
137  '' Special case, allow $LANG metacommand in all dialects
138  if( hMatchText( "LANG" ) ) then
139 
140  '' ':'
141  if( hMatch( FB_TK_STMTSEP ) = FALSE ) then
142  errReport( FB_ERRMSG_EXPECTEDSTMTSEP, TRUE )
143  exit select
144  end if
145 
146  '' "STR_LIT"
147  if( lexGetClass( ) = FB_TKCLASS_STRLITERAL ) then
148  dim as FB_LANG id = any
149 
150  lexEatToken( incfile )
151 
152  id = fbGetLangId( @incfile )
153 
154  if( id = FB_LANG_INVALID ) then
155  errReport( FB_ERRMSG_INVALIDLANG )
156  else
157  fbChangeOption( FB_COMPOPT_LANG, id )
158 
159  '' Preserve under -pp
160  if( env.ppfile_num > 0 ) then
161  lexPPOnlyEmitText( "'$lang: """ + fbGetLangName( id ) + """" )
162  end if
163  end if
164  else
165  errReport( FB_ERRMSG_SYNTAXERROR )
166  end if
167  end if
168  end select
169  end select
170 
171  '' skip until next line
172  do
173  select case lexGetToken( )
174  case FB_TK_EOL, FB_TK_EOF
175  exit do
176  end select
177 
178  lexSkipToken( )
179  loop
180 end sub
181