Skip to content

Commit 3af2d70

Browse files
committed
wip: parser: Fix hMatchText-based keyword checks to only allow identifiers
and not string literals or similar. http://www.freebasic.net/forum/viewtopic.php?f=3&t=23978
1 parent 5407d98 commit 3af2d70

17 files changed

+115
-56
lines changed

changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Version 1.05.0
1313
- ld was warning about unsupported linker options on OS X
1414
- BYREF fixed-length strings (e.g. BYREF myParameter AS ZSTRING * 10) now trigger a compiler error since they are unsupported
1515
- #print typeof() output now differentiates between ZSTRING and ZSTRING * N (ZSTRING without size is produced by dereferencing a ZSTRING PTR, or BYREF AS ZSTRING)
16+
- Context-specific keywords, e.g. graphics PUT modes, must now be given as keywords (e.g. PSET), string literals (e.g. "PSET") are no longer accepted.
1617

1718

1819
Version 1.04.0

src/compiler/hlp.bas

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,15 @@
99
#include once "lex.bi"
1010
#include once "dstr.bi"
1111

12-
'':::::
13-
function hMatchText _
14-
( _
15-
byval txt as zstring ptr _
16-
) as integer
17-
18-
if( ucase( *lexGetText( ) ) = *txt ) then
19-
lexSkipToken( )
20-
function = TRUE
21-
else
22-
function = FALSE
23-
end if
24-
12+
function hMatchIdOrKw( byval txt as const zstring ptr ) as integer
13+
select case( lexGetClass( ) )
14+
case FB_TKCLASS_IDENTIFIER, FB_TKCLASS_QUIRKWD, FB_TKCLASS_KEYWORD
15+
if( ucase( *lexGetText( ) ) = *txt ) then
16+
lexSkipToken( )
17+
return TRUE
18+
end if
19+
end select
20+
return FALSE
2521
end function
2622

2723
'':::::

src/compiler/hlp.bi

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@ const INVALID = -1
99
'' helper module protos
1010
''
1111

12-
declare function hMatchText _
13-
( _
14-
byval txt as zstring ptr _
15-
) as integer
12+
declare function hMatchIdOrKw( byval txt as const zstring ptr ) as integer
1613

1714
declare function hMatch _
1815
( _

src/compiler/parser-comment.bas

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,7 @@ private sub cDirective( ) static
9090
lexSkipToken( )
9191

9292
'' ONCE?
93-
isonce = FALSE
94-
if( hMatchText( "ONCE" ) ) then
95-
isonce = TRUE
96-
end if
93+
isonce = hMatchIdOrKw( "ONCE" )
9794

9895
'' ':'
9996
if( hMatch( FB_TK_STMTSEP ) = FALSE ) then
@@ -135,7 +132,7 @@ private sub cDirective( ) static
135132

136133
case else
137134
'' Special case, allow $LANG metacommand in all dialects
138-
if( hMatchText( "LANG" ) ) then
135+
if( hMatchIdOrKw( "LANG" ) ) then
139136

140137
'' ':'
141138
if( hMatch( FB_TK_STMTSEP ) = FALSE ) then

src/compiler/parser-quirk-file.bas

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -991,20 +991,16 @@ private function hFileOpen _
991991
end if
992992

993993
'' (ACCESS (READ|WRITE|READ WRITE))?
994-
if( hMatchText( "ACCESS" ) ) then
995-
select case ucase( *lexGetText( ) )
996-
case "WRITE"
997-
lexSkipToken( )
994+
if( hMatchIdOrKw( "ACCESS" ) ) then
995+
if( hMatchIdOrKw( "WRITE" ) ) then
998996
access_mode = FB_FILE_ACCESS_WRITE
999-
1000-
case "READ"
1001-
lexSkipToken( )
997+
elseif( hMatchIdOrKw( "READ" ) ) then
1002998
if( hMatch( FB_TK_WRITE ) ) then
1003999
access_mode = FB_FILE_ACCESS_READWRITE
10041000
else
10051001
access_mode = FB_FILE_ACCESS_READ
10061002
end if
1007-
end select
1003+
end if
10081004
else
10091005
access_mode = FB_FILE_ACCESS_ANY
10101006
end if
@@ -1020,20 +1016,16 @@ private function hFileOpen _
10201016
if( hMatch( FB_TK_SHARED ) ) then
10211017
lock_mode = FB_FILE_LOCK_SHARED
10221018

1023-
elseif( hMatchText( "LOCK" ) ) then
1024-
select case ucase( *lexGetText( ) )
1025-
case "WRITE"
1026-
lexSkipToken( )
1019+
elseif( hMatchIdOrKw( "LOCK" ) ) then
1020+
if( hMatchIdOrKw( "WRITE" ) ) then
10271021
lock_mode = FB_FILE_LOCK_WRITE
1028-
1029-
case "READ"
1030-
lexSkipToken( )
1022+
elseif( hMatchIdOrKw( "READ" ) ) then
10311023
if( hMatch( FB_TK_WRITE ) ) then
10321024
lock_mode = FB_FILE_LOCK_READWRITE
10331025
else
10341026
lock_mode = FB_FILE_LOCK_READ
10351027
end if
1036-
end select
1028+
end if
10371029
else
10381030
lock_mode = FB_FILE_LOCK_SHARED
10391031
end if
@@ -1060,7 +1052,7 @@ private function hFileOpen _
10601052
end if
10611053

10621054
'' (LEN '=' Expression)?
1063-
if( hMatchText( "LEN" ) ) then
1055+
if( hMatchIdOrKw( "LEN" ) ) then
10641056
if( cAssignToken( ) = FALSE ) then
10651057
errReport( FB_ERRMSG_EXPECTEDEQ )
10661058
flen = astNewCONSTi( 0 )

src/compiler/parser-quirk-gfx.bas

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -282,52 +282,59 @@ private function hGetMode _
282282

283283
function = FALSE
284284

285-
select case as const lexGetToken
286-
285+
select case( lexGetToken( ) )
287286
case FB_TK_AND
288287
lexSkipToken
289288
mode = FBGFX_PUTMODE_AND
289+
return TRUE
290290

291291
case FB_TK_OR
292292
lexSkipToken
293293
mode = FBGFX_PUTMODE_OR
294+
return TRUE
294295

295296
case FB_TK_XOR
296297
lexSkipToken
297298
mode = FBGFX_PUTMODE_XOR
299+
return TRUE
300+
end select
298301

299-
case else
302+
select case( lexGetClass( ) )
303+
case FB_TKCLASS_IDENTIFIER, FB_TKCLASS_QUIRKWD, FB_TKCLASS_KEYWORD
300304
select case ucase( *lexGetText( ) )
301305
case "PSET"
302306
lexSkipToken( )
303307
mode = FBGFX_PUTMODE_PSET
308+
return TRUE
304309

305310
case "PRESET"
306311
lexSkipToken( )
307312
mode = FBGFX_PUTMODE_PRESET
313+
return TRUE
308314

309315
case "TRANS"
310316
lexSkipToken( )
311317
mode = FBGFX_PUTMODE_TRANS
318+
return TRUE
312319

313320
case "ADD"
314321
lexSkipToken( )
315322
mode = FBGFX_PUTMODE_ADD
316-
317323
if( hMatch( CHAR_COMMA ) ) then
318324
hMatchExpression( alphaexpr )
319325
else
320326
alphaexpr = astNewCONSTi( 255, FB_DATATYPE_UINT )
321327
end if
328+
return TRUE
322329

323330
case "ALPHA"
324331
lexSkipToken( )
325332
mode = FBGFX_PUTMODE_ALPHA
326-
327333
if( hMatch( CHAR_COMMA ) ) then
328334
hMatchExpression( alphaexpr )
329335
mode = FBGFX_PUTMODE_BLEND
330336
end if
337+
return TRUE
331338

332339
case "CUSTOM"
333340
lexSkipToken( )
@@ -377,14 +384,13 @@ private function hGetMode _
377384
exit function
378385
end if
379386

380-
case else
381-
errReport( FB_ERRMSG_SYNTAXERROR )
382-
exit function
387+
return TRUE
383388
end select
384-
end select
385389

386-
function = TRUE
390+
end select
387391

392+
errReport( FB_ERRMSG_SYNTAXERROR )
393+
return FALSE
388394
end function
389395

390396
'':::::
@@ -877,7 +883,7 @@ function cGfxPalette as integer
877883

878884
function = FALSE
879885

880-
isget = hMatchText( "GET" )
886+
isget = hMatchIdOrKw( "GET" )
881887

882888
'' this could fail if using was #undef'ed and made a method...
883889
if( hMatch( FB_TK_USING ) ) then

src/compiler/pp.bas

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -306,12 +306,7 @@ private sub ppInclude()
306306
dim as integer isonce = any
307307

308308
'' ONCE?
309-
isonce = FALSE
310-
if( lexGetClass( ) = FB_TKCLASS_IDENTIFIER ) then
311-
if( hMatchText( "ONCE" ) ) then
312-
isonce = TRUE
313-
end if
314-
end if
309+
isonce = hMatchIdOrKw( "ONCE" )
315310

316311
if( lexGetClass( ) <> FB_TKCLASS_STRLITERAL ) then
317312
errReport( FB_ERRMSG_SYNTAXERROR )
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
' TEST_MODE : COMPILE_ONLY_FAIL
2+
3+
dim image as any ptr
4+
put (0, 0), image, "add"
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
' TEST_MODE : COMPILE_ONLY_FAIL
2+
3+
dim image as any ptr
4+
put (0, 0), image, "alpha"
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
' TEST_MODE : COMPILE_ONLY_FAIL
2+
3+
dim image as any ptr
4+
put (0, 0), image, "and"

0 commit comments

Comments
 (0)