Skip to content

Commit

Permalink
More newlines are correctly handled
Browse files Browse the repository at this point in the history
  • Loading branch information
kmizu committed Nov 16, 2024
1 parent f22b3e0 commit cc85f2a
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 18 deletions.
46 changes: 34 additions & 12 deletions grammar/JJOnionParser.jj
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -127,12 +128,32 @@ public class JJOnionParser implements Parser {
return new String(b);
}

private Stack<Integer> states = new Stack<Integer>();

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)
Expand Down Expand Up @@ -561,12 +582,11 @@ AST.MethodDeclaration method_decl(int mset) : {
AST.BlockExpression b = null;
AST.Expression e = null;
}{
{enterSection();}
"def" t=<ID> ["(" 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)))
Expand Down Expand Up @@ -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;}
}
Expand Down Expand Up @@ -923,14 +943,16 @@ AST.ClosureExpression anonymous_function() :{
List<AST.Argument> args = (List<AST.Argument>)AST.NIL();
AST.BlockExpression body;
}{
t="#" [ ty=class_type() "." n=<ID>] ["(" args=args() ")"] body=block() {
{enterDefault();}
t="#" [ ty=class_type() "." n=<ID>] ["(" args=args() ")"] body=block() {
String mname;
if(ty == null) {
ty = new AST.TypeNode(p(t), new AST.ReferenceType("onion.Function" + args.size(), true), true);
mname = "call";
}else {
mname = n.image;
}
leaveDefault();
return new AST.ClosureExpression(p(t), ty, mname, args, null, body);
}
}
Expand Down
14 changes: 8 additions & 6 deletions src/test/scala/onion/compiler/tools/CountersSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit cc85f2a

Please sign in to comment.