From fff0d741c3dd432e07026907b67c7d1fd5514bf7 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Wed, 2 Oct 2024 18:58:55 +0200 Subject: [PATCH] improved parsing of "not in" operator, and [] array signature (allow space) --- compiler/test/ast/TestProg8Parser.kt | 4 ++-- compilerAst/src/prog8/ast/antlr/Antlr2Kotlin.kt | 12 ++++++++++-- docs/source/todo.rst | 5 ++--- examples/test.p8 | 2 +- parser/antlr/Prog8ANTLR.g4 | 8 ++++---- 5 files changed, 19 insertions(+), 12 deletions(-) diff --git a/compiler/test/ast/TestProg8Parser.kt b/compiler/test/ast/TestProg8Parser.kt index 495724ef7..6ab778b44 100644 --- a/compiler/test/ast/TestProg8Parser.kt +++ b/compiler/test/ast/TestProg8Parser.kt @@ -884,7 +884,7 @@ class TestProg8Parser: FunSpec( { main { sub start() { str string = "hello" - ubyte[] array = [1,2,3,4] + ubyte[ ] array = [1,2,3,4] bool bb ubyte cc @@ -915,7 +915,7 @@ class TestProg8Parser: FunSpec( { main { sub start() { bool @shared cc - ubyte[] array = [1,2,3] + ubyte [ ] array = [1,2,3] cc = 99 in 12345 cc = 9999 in array } diff --git a/compilerAst/src/prog8/ast/antlr/Antlr2Kotlin.kt b/compilerAst/src/prog8/ast/antlr/Antlr2Kotlin.kt index 1081e7fd7..4dea07851 100644 --- a/compilerAst/src/prog8/ast/antlr/Antlr2Kotlin.kt +++ b/compilerAst/src/prog8/ast/antlr/Antlr2Kotlin.kt @@ -503,8 +503,16 @@ private fun ExpressionContext.toAst(insideParentheses: Boolean=false) : Expressi if(scoped_identifier()!=null) return scoped_identifier().toAst() - if(bop!=null) - return BinaryExpression(left.toAst(), bop.text.trim(), right.toAst(), toPosition(), insideParentheses=insideParentheses) + if(bop!=null) { + val operator = bop.text.trim().replace("\\s+".toRegex(), " ") + return BinaryExpression( + left.toAst(), + operator, + right.toAst(), + toPosition(), + insideParentheses = insideParentheses + ) + } if(prefix!=null) return PrefixExpression(prefix.text, expression(0).toAst(), toPosition()) diff --git a/docs/source/todo.rst b/docs/source/todo.rst index 348d77eef..0f3d45291 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -1,10 +1,9 @@ TODO ==== -fix the ubyte[] parser error. +word starw; for starw in 50 downto 0 -> compiler error word loop variable can only loop over bytes or words -word starw -for starw in 50 downto 0 -> compiler error word loop variable can only loop over bytes or words +chess prg got bigger again. why? Regenerate skeleton doc files. diff --git a/examples/test.p8 b/examples/test.p8 index ff728e2be..06cc5485b 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -16,7 +16,7 @@ main { ubyte[] stuff1=[1,2,3] ubyte [] stuff2=[1,2,3] - ubyte[ ] stuff3=[1,2,3] ; TODO fix parse error + ubyte[ ] stuff3=[1,2,3] stuff1[1]++ stuff2[1]++ stuff3[1]++ diff --git a/parser/antlr/Prog8ANTLR.g4 b/parser/antlr/Prog8ANTLR.g4 index 6ba39f703..841cd4b50 100644 --- a/parser/antlr/Prog8ANTLR.g4 +++ b/parser/antlr/Prog8ANTLR.g4 @@ -63,9 +63,9 @@ SHARED : '@shared' ; SPLIT: '@split' ; -ARRAYSIG : - '[]' - ; +ARRAYSIG : '[' [ \t]* ']' ; + +NOT_IN: 'not' [ \t]+ 'in' [ \t] ; // A module (file) consists of zero or more directives or blocks, in any order. @@ -184,7 +184,7 @@ expression : | left = expression EOL? bop = ('==' | '!=') EOL? right = expression | rangefrom = expression rto = ('to'|'downto') rangeto = expression ('step' rangestep = expression)? // can't create separate rule due to mutual left-recursion | left = expression EOL? bop = 'in' EOL? right = expression - | left = expression EOL? bop = ('not in ' | 'not in\t' | 'not in\n' | 'not in\r') EOL? right = expression + | left = expression EOL? bop = NOT_IN EOL? right = expression | prefix = 'not' expression | left = expression EOL? bop = 'and' EOL? right = expression | left = expression EOL? bop = 'or' EOL? right = expression