X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;ds=sidebyside;f=bundles%2Forg.simantics.scl.compiler%2Fsrc%2Forg%2Fsimantics%2Fscl%2Fcompiler%2Finternal%2Fparsing%2Fparser%2FSCLParser.java;fp=bundles%2Forg.simantics.scl.compiler%2Fsrc%2Forg%2Fsimantics%2Fscl%2Fcompiler%2Finternal%2Fparsing%2Fparser%2FSCLParser.java;h=6c8ee5be9c0de160fd7123f0027c9dc5a9ff18d4;hb=649890ad306df48440a97893d7d53fb8a6386a4e;hp=532f6e8a3f62941644a6c57a501e7a13fa08381e;hpb=655590362c7017aff657d1ff30e6c63f03b6dd75;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/parsing/parser/SCLParser.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/parsing/parser/SCLParser.java index 532f6e8a3..6c8ee5be9 100644 --- a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/parsing/parser/SCLParser.java +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/parsing/parser/SCLParser.java @@ -252,18 +252,22 @@ public abstract class SCLParser { protected abstract RuntimeException syntaxError(Token token, String description); - private static String describeAction(int action) { + private static String describeAction(boolean isGoto, int action) { if(action == ERROR_ACTION) return "ERROR"; if(action == ACCEPT_ACTION) return "ACCEPT"; StringBuilder b = new StringBuilder(); - if((action & REDUCE_MASK) != 0) { - action ^= REDUCE_MASK; - b.append("REDUCE"); + if(isGoto) + b.append("GOTO "); + else { + if((action & REDUCE_MASK) != 0) { + action ^= REDUCE_MASK; + b.append("REDUCE"); + } + else + b.append("SHIFT"); } - else - b.append("SHIFT"); if((action & POP_MASK) != 0) { action ^= POP_MASK; b.append(" POP"); @@ -276,34 +280,46 @@ public abstract class SCLParser { return b.toString(); } + private void printState(int state) { + System.out.print("state=" + state + ":"); + for(int i=symbolStackLength-1,j=stateStackLength-1;i>=0;--i) { + Object s = symbolStack[i]; + if(s instanceof Token) + System.out.print(" " + TERMINAL_NAMES[((Token)s).id]); + else if(s == null) + System.out.print(" null"); + else + System.out.print(" " + s.getClass().getSimpleName()); + while(j>=0 && symbolStackPositionStack[j]==i) + System.out.print(" (" + stateStack[j--] + ")"); + } + System.out.println(); + } + private Object parse(int state) { while(true) { Token token = nextToken(); int tokenId = token.id; + if(TRACE) + System.out.println("---> token " + TERMINAL_NAMES[tokenId] + " \"" + token.text + "\" <---"); while(true) { + if(TRACE) + printState(state); short action = getAction(state, tokenId); - if(TRACE) { - System.out.println("state=" + state + ", tokenId=" + TERMINAL_NAMES[tokenId] + - ", action=" + describeAction(action)); - System.out.print(" "); - for(int i=symbolStackLength-1,j=stateStackLength-1;i>=0;--i) { - Object s = symbolStack[i]; - if(s instanceof Token) - System.out.print(" " + TERMINAL_NAMES[((Token)s).id]); - else - System.out.print(" " + s.getClass().getSimpleName()); - while(j>=0 && symbolStackPositionStack[j]==i) - System.out.print(" (" + stateStack[j--] + ")"); - } - System.out.println(); - } + if(TRACE) + System.out.println(" -> action=" + describeAction(false, action)); //System.out.println(STATE_DESCRIPTIONS[state]); if((action & REDUCE_MASK) != 0) { if(action == ACCEPT_ACTION) return symbolStack[symbolStackLength-1]; if(action == ERROR_ACTION) throw syntaxError(token, parseErrorDescription(state, token, tokenId)); - stateStackLength -= (action >>> 13)&3; + int popAmount = (action >>> 13)&3; + if(TRACE) { + if(popAmount > 0) + System.out.println(" POP " + popAmount); + } + stateStackLength -= popAmount; action &= STATE_MASK; int reductionBegin = symbolStackPositionStack[--stateStackLength]; @@ -318,7 +334,16 @@ public abstract class SCLParser { symbolStack[symbolStackLength] = symbol; state = stateStack[stateStackLength]; + if(TRACE) { + ++symbolStackLength; + printState(state); + --symbolStackLength; + System.out.println(" nonterminal=" + NONTERMINAL_NAMES[PRODUCT_LHS[action]]); + } action = getGoto(state, PRODUCT_LHS[action]); + if(TRACE) + System.out.println(" -> action=" + describeAction(true, action)); + // Pop state if((action & POP_MASK) != 0) { --stateStackLength; @@ -637,7 +662,7 @@ public abstract class SCLParser { case 124: return reduceApplyType(); case 125: - return reduceDummy1(); + return reduceDummy(); default: throw new RuntimeException("Internal parser error."); @@ -1136,7 +1161,7 @@ public abstract class SCLParser { /** * dummy ::= COMMENT EOL */ - protected abstract Object reduceDummy1(); + protected abstract Object reduceDummy(); protected void postReduce(Object reduced) { }