From cc85f2a6169f229da24dbbeaf7c6678a6a25992f Mon Sep 17 00:00:00 2001 From: Kota Mizushima Date: Sun, 17 Nov 2024 00:34:37 +0900 Subject: [PATCH] More newlines are correctly handled --- grammar/JJOnionParser.jj | 46 ++++++++++++++----- .../onion/compiler/tools/CountersSpec.scala | 14 +++--- 2 files changed, 42 insertions(+), 18 deletions(-) diff --git a/grammar/JJOnionParser.jj b/grammar/JJOnionParser.jj index 1c7aae5..22b8daf 100644 --- a/grammar/JJOnionParser.jj +++ b/grammar/JJOnionParser.jj @@ -22,6 +22,7 @@ import scala.collection.mutable.Buffer; import scala.Option; import scala.Option$; import static onion.compiler.AST.append; +import java.util.Stack; @SuppressWarnings("unchecked") public class JJOnionParser implements Parser { @@ -127,12 +128,32 @@ public class JJOnionParser implements Parser { return new String(b); } + private Stack states = new Stack(); + + private void enterState(int state) { + int oldState = token_source.curLexState; + states.push(oldState); + token_source.SwitchTo(state); + } + + private void leaveState() { + token_source.SwitchTo(states.pop()); + } + + private void enterDefault() { + enterState(DEFAULT); + } + + private void leaveDefault() { + leaveState(); + } + private void enterSection() { - token_source.SwitchTo(IN_STATEMENT); + enterState(IN_STATEMENT); } private void leaveSection() { - token_source.SwitchTo(DEFAULT); + leaveState(); } } PARSER_END(JJOnionParser) @@ -561,12 +582,11 @@ AST.MethodDeclaration method_decl(int mset) : { AST.BlockExpression b = null; AST.Expression e = null; }{ - {enterSection();} "def" t= ["(" args=args() ")"] [":" ty=return_type()] - ( ({leaveSection();} b=block() | eos() ) { + ( (b=block() | {enterSection();} eos() ) { return new AST.MethodDeclaration(p(t), mset, t.image, args, ty, b); } - | ("=" eols() e=term() eos()) { + | ({enterSection();} "=" eols() e=term() eos()) { return new AST.MethodDeclaration( p(t), mset, t.image, args, ty, new AST.BlockExpression(p(t), asList(new AST.ReturnExpression(p(t), e))) @@ -785,12 +805,12 @@ AST.Expression term() :{AST.Expression e;}{ AST.Expression assignable() :{Token t; AST.Expression a, b; }{ a=logical_or() - [ ( t="=" b=assignable() {a = new AST.Assignment(p(t), a, b);} - | t="+=" b=assignable() {a = new AST.AdditionAssignment(p(t), a, b);} - | t="-=" b=assignable() {a = new AST.SubtractionAssignment(p(t), a, b);} - | t="*=" b=assignable() {a = new AST.MultiplicationAssignment(p(t), a, b);} - | t="/=" b=assignable() {a = new AST.DivisionAssignment(p(t), a, b);} - | t="%=" b=assignable() {a = new AST.ModuloAssignment(p(t), a, b);} + [ ( t="=" eols() b=assignable() {a = new AST.Assignment(p(t), a, b);} + | t="+=" eols() b=assignable() {a = new AST.AdditionAssignment(p(t), a, b);} + | t="-=" eols() b=assignable() {a = new AST.SubtractionAssignment(p(t), a, b);} + | t="*=" eols() b=assignable() {a = new AST.MultiplicationAssignment(p(t), a, b);} + | t="/=" eols() b=assignable() {a = new AST.DivisionAssignment(p(t), a, b);} + | t="%=" eols() b=assignable() {a = new AST.ModuloAssignment(p(t), a, b);} ) ]{return a;} } @@ -923,7 +943,8 @@ AST.ClosureExpression anonymous_function() :{ List args = (List)AST.NIL(); AST.BlockExpression body; }{ - t="#" [ ty=class_type() "." n=] ["(" args=args() ")"] body=block() { + {enterDefault();} + t="#" [ ty=class_type() "." n=] ["(" args=args() ")"] body=block() { String mname; if(ty == null) { ty = new AST.TypeNode(p(t), new AST.ReferenceType("onion.Function" + args.size(), true), true); @@ -931,6 +952,7 @@ AST.ClosureExpression anonymous_function() :{ }else { mname = n.image; } + leaveDefault(); return new AST.ClosureExpression(p(t), ty, mname, args, null, body); } } diff --git a/src/test/scala/onion/compiler/tools/CountersSpec.scala b/src/test/scala/onion/compiler/tools/CountersSpec.scala index 4fb07d7..e6de918 100644 --- a/src/test/scala/onion/compiler/tools/CountersSpec.scala +++ b/src/test/scala/onion/compiler/tools/CountersSpec.scala @@ -14,14 +14,16 @@ class CountersSpec extends AbstractShellSpec { | class Counters { | public: | static def counter(begin :Int, up :Int) :Counter = - | #Counter.count { return begin = begin + up; }; + | #Counter.count { + | return begin = + | begin + up + | } | - | static def id(x: Int): Int = x | static def main(args: String[]): Int { - | c = counter(1, 10); - | c.count; - | c.count; - | return c.count; + | c = counter(1, 10) + | c.count + | c.count + | return c.count | } |} |""".stripMargin,