diff --git a/.editorconfig b/.editorconfig index a45db48e146596da3c7d69b124388500998ed5f3..82159d830579e0d0e73920bcbc72f85178739e17 100644 --- a/.editorconfig +++ b/.editorconfig @@ -5,3 +5,7 @@ indent_style = space indent_size = 2 charset = utf-8 insert_final_newline = true + +[*.txt] +indent_size = 4 +trim_trailing_whitespace = true diff --git a/src/main/resources/org/sablecc/sablecc/alternatives.txt b/src/main/resources/org/sablecc/sablecc/alternatives.txt index cf6e4952d37a198f84371eabca9a69e559af7bee..123ba617ee40bf3f02ba1db8775e1e957cb57065 100644 --- a/src/main/resources/org/sablecc/sablecc/alternatives.txt +++ b/src/main/resources/org/sablecc/sablecc/alternatives.txt @@ -21,7 +21,7 @@ $ Macro:AlternativeHeader2 import $0$.*; - + public final class $1$ extends $2$ { diff --git a/src/main/resources/org/sablecc/sablecc/lexer.txt b/src/main/resources/org/sablecc/sablecc/lexer.txt index 776e506a9709555c720fe4951860e05608c8e95b..3e9a5b324273688d3dcd9bf71322abe45849b6a5 100644 --- a/src/main/resources/org/sablecc/sablecc/lexer.txt +++ b/src/main/resources/org/sablecc/sablecc/lexer.txt @@ -32,7 +32,7 @@ import $1$.*; import de.hhu.stups.sablecc.patch.*; import java.util.concurrent.LinkedBlockingQueue; -@SuppressWarnings({"unused"}) +@SuppressWarnings({"unused"}) public class Lexer implements ITokenListContainer { protected Token token; @@ -44,31 +44,31 @@ public class Lexer implements ITokenListContainer private boolean cr; private boolean eof; private final StringBuffer text = new StringBuffer(); - - private List<IToken> tokenList; + + private List<IToken> tokenList; private final Queue<IToken> nextList = new LinkedBlockingQueue<IToken>(); - private IToken tok; + private IToken tok; public Queue<IToken> getNextList() { return nextList; } - public List<IToken> getTokenList() { - return tokenList; - } - - private void setToken(Token t) { - tok = t; - token = t; - } - - - public void setTokenList(final List<IToken> list) { - tokenList = list; - } - - + public List<IToken> getTokenList() { + return tokenList; + } + + private void setToken(Token t) { + tok = t; + token = t; + } + + + public void setTokenList(final List<IToken> list) { + tokenList = list; + } + + protected void filter() throws LexerException, IOException { // Do nothing @@ -76,20 +76,20 @@ public class Lexer implements ITokenListContainer protected void filterWrap() throws LexerException, IOException { - filter(); - if (token != null) { - getTokenList().add(token); - nextList.add(token); - } + filter(); + if (token != null) { + getTokenList().add(token); + nextList.add(token); + } } public Lexer( PushbackReader in) { this.in = in; - setTokenList(new ArrayList<IToken>()); + setTokenList(new ArrayList<IToken>()); } - + public Token peek() throws LexerException, IOException { while(this.token == null) @@ -410,7 +410,7 @@ $ Macro:LexerTail - static + static { try { diff --git a/src/main/resources/org/sablecc/sablecc/parser.txt b/src/main/resources/org/sablecc/sablecc/parser.txt index 8d1dc232c77fce1985f3083ea3ad094acbfa4032..82ef6f2445421f70a5864d0435545ad227907bf4 100644 --- a/src/main/resources/org/sablecc/sablecc/parser.txt +++ b/src/main/resources/org/sablecc/sablecc/parser.txt @@ -38,7 +38,7 @@ public class Parser implements IParser private final static int REDUCE = 1; private final static int ACCEPT = 2; private final static int ERROR = 3; - + protected ArrayList firstPopped = null; protected ArrayList lastPopped = null; private ITokenListContainer lex; @@ -46,150 +46,150 @@ public class Parser implements IParser public Parser( Lexer lexer) { this.lexer = lexer; - this.lex = lexer; + this.lex = lexer; } - - - private Map<PositionedNode, SourcecodeRange> mapping = new HashMap<PositionedNode, SourcecodeRange>(); - public Map<PositionedNode, SourcecodeRange> getMapping() { return this.mapping; } + + + private Map<PositionedNode, SourcecodeRange> mapping = new HashMap<PositionedNode, SourcecodeRange>(); + public Map<PositionedNode, SourcecodeRange> getMapping() { return this.mapping; } protected void checkResult(Object elementToCheck) { - checkResult(elementToCheck, false); + checkResult(elementToCheck, false); + } + + + protected void checkResult(Object elementToCheck, boolean slurp) { + // nodes with no tokens or sub nodes at all may exist + if (this.firstPopped == null) { + return; + } + + if (elementToCheck instanceof LinkedList) { + /* + * special case: this is a list of nodes, for example an identifier + * list, so we don't want to check the list but the last element + * added to it + */ + final LinkedList nodeList = (LinkedList) elementToCheck; + + if (nodeList.size() > 0) { + elementToCheck = nodeList.get(nodeList.size() - 1); + } else { + // no positions for empty lists... + return; + } + } + + if (!(elementToCheck instanceof PositionedNode)) { + throw new Error( + "Unexpected elementToCheck (not instanceof PositionedNode): " + + elementToCheck.getClass().getSimpleName() + "/" + + elementToCheck); + } + + if (!this.getMapping().containsKey(elementToCheck) || slurp ) { + final PositionedNode node = (PositionedNode) elementToCheck; + + // dealing with a one-token element + if (this.lastPopped == null) { + this.lastPopped = this.firstPopped; + } + + final int begin = findBeginPos(this.lastPopped, node); + int end = findEndPos(this.firstPopped); + if (end == -1) end = begin; + final SourcecodeRange range = new SourcecodeRange(begin, end); + + this.getMapping().put(node, range); + + node.setStartPos(createBeginPos(begin)); + node.setEndPos(createEndPos(end)); + } + } + + + protected int findBeginPos(final ArrayList list, + PositionedNode n) { + Object first = list.get(0); + if (!(first instanceof PositionedNode) && !(first instanceof IToken)) { + List list2 = (List) first; + + if (list2.size() > 0) { + first = list2.get(0); + } else { + /* + * Sometimes (haven't found out why) we get empty list here. In + * the only observed cases we were looking for the source range + * of the whole parse unit. Then the index is 0. + */ + return 0; + } + } + + if (first instanceof IToken) { + return findIndex((IToken) first); + } + + final PositionedNode node = (PositionedNode) first; + final SourcecodeRange item = this.getMapping().get(node); + if (item == null){ + System.err.println(n.getClass().getSimpleName() + " / " + node.getClass().getSimpleName() + ": " + node); + } + return item.getBeginIndex(); + } + + + protected int findEndPos(final ArrayList list) { + Object last = list.get(list.size() - 1); + if (!(last instanceof PositionedNode) && !(last instanceof IToken)) { + final List list2 = (List) last; + last = list2.get(list2.size() - 1); + } + + if (last instanceof IToken) { + return findIndex((IToken) last); + } + + final PositionedNode node = (PositionedNode) last; + final SourcecodeRange item = this.getMapping().get(node); + if (item == null) + return -1; + return item.getEndIndex(); + } + + protected int findIndex(final IToken token) { + final List<IToken> list = this.lex.getTokenList(); + + for (int i = list.size() - 1; i >= 0; i--) { + if (list.get(i) == token) { + return i; + } + } + + return -1; + } + + protected SourcePosition createBeginPos(final int index) { + final List<IToken> list = this.lex.getTokenList(); + final IToken token = list.get(index); + return new SourcePosition(token.getLine(), token.getPos()); + } + + protected SourcePosition createEndPos(final int index) { + final List<IToken> list = this.lex.getTokenList(); + final IToken token = list.get(index); + return new SourcePosition(token.getLine(), token.getPos() + + token.getText().length()); + } + + protected boolean addElementsFromListToNewList(String productionRuleAsString) { + return true; } - - protected void checkResult(Object elementToCheck, boolean slurp) { - // nodes with no tokens or sub nodes at all may exist - if (this.firstPopped == null) { - return; - } - - if (elementToCheck instanceof LinkedList) { - /* - * special case: this is a list of nodes, for example an identifier - * list, so we don't want to check the list but the last element - * added to it - */ - final LinkedList nodeList = (LinkedList) elementToCheck; - - if (nodeList.size() > 0) { - elementToCheck = nodeList.get(nodeList.size() - 1); - } else { - // no positions for empty lists... - return; - } - } - - if (!(elementToCheck instanceof PositionedNode)) { - throw new Error( - "Unexpected elementToCheck (not instanceof PositionedNode): " - + elementToCheck.getClass().getSimpleName() + "/" - + elementToCheck); - } - - if (!this.getMapping().containsKey(elementToCheck) || slurp ) { - final PositionedNode node = (PositionedNode) elementToCheck; - - // dealing with a one-token element - if (this.lastPopped == null) { - this.lastPopped = this.firstPopped; - } - - final int begin = findBeginPos(this.lastPopped, node); - int end = findEndPos(this.firstPopped); - if (end == -1) end = begin; - final SourcecodeRange range = new SourcecodeRange(begin, end); - - this.getMapping().put(node, range); - - node.setStartPos(createBeginPos(begin)); - node.setEndPos(createEndPos(end)); - } - } - - - protected int findBeginPos(final ArrayList list, - PositionedNode n) { - Object first = list.get(0); - if (!(first instanceof PositionedNode) && !(first instanceof IToken)) { - List list2 = (List) first; - - if (list2.size() > 0) { - first = list2.get(0); - } else { - /* - * Sometimes (haven't found out why) we get empty list here. In - * the only observed cases we were looking for the source range - * of the whole parse unit. Then the index is 0. - */ - return 0; - } - } - - if (first instanceof IToken) { - return findIndex((IToken) first); - } - - final PositionedNode node = (PositionedNode) first; - final SourcecodeRange item = this.getMapping().get(node); - if (item == null){ - System.err.println(n.getClass().getSimpleName() + " / " + node.getClass().getSimpleName() + ": " + node); - } - return item.getBeginIndex(); - } - - - protected int findEndPos(final ArrayList list) { - Object last = list.get(list.size() - 1); - if (!(last instanceof PositionedNode) && !(last instanceof IToken)) { - final List list2 = (List) last; - last = list2.get(list2.size() - 1); - } - - if (last instanceof IToken) { - return findIndex((IToken) last); - } - - final PositionedNode node = (PositionedNode) last; - final SourcecodeRange item = this.getMapping().get(node); - if (item == null) - return -1; - return item.getEndIndex(); - } - - protected int findIndex(final IToken token) { - final List<IToken> list = this.lex.getTokenList(); - - for (int i = list.size() - 1; i >= 0; i--) { - if (list.get(i) == token) { - return i; - } - } - - return -1; - } - - protected SourcePosition createBeginPos(final int index) { - final List<IToken> list = this.lex.getTokenList(); - final IToken token = list.get(index); - return new SourcePosition(token.getLine(), token.getPos()); - } - - protected SourcePosition createEndPos(final int index) { - final List<IToken> list = this.lex.getTokenList(); - final IToken token = list.get(index); - return new SourcePosition(token.getLine(), token.getPos() - + token.getText().length()); - } - - protected boolean addElementsFromListToNewList(String productionRuleAsString) { - return true; - } - $ Macro:ParserInliningPushHeader - + private void push(int numstate, ArrayList listNode) throws ParserException, LexerException, IOException { this.nodeList = listNode; @@ -265,12 +265,12 @@ Macro:ParserCommon protected ArrayList pop() { - ArrayList list = ((State) this.stack.previous()).nodes; - if (this.firstPopped == null) { - this.firstPopped = list; - } else { - this.lastPopped = list; - } + ArrayList list = ((State) this.stack.previous()).nodes; + if (this.firstPopped == null) { + this.firstPopped = list; + } else { + this.lastPopped = list; + } return list; } @@ -281,11 +281,11 @@ Macro:ParserCommon return this.converter.index; } - + public Start parse() throws ParserException, LexerException, IOException { - this.getMapping().clear(); - + this.getMapping().clear(); + push(0, null$0$); while(true) { @@ -329,12 +329,12 @@ Macro:ParserCommon switch(this.action[0]) { case SHIFT: - { - ArrayList list = new ArrayList(); - list.add(this.lexer.next()); + { + ArrayList list = new ArrayList(); + list.add(this.lexer.next()); push(this.action[1], list$1$); } - break; + break; case REDUCE: switch(this.action[1]) { @@ -343,21 +343,21 @@ $ Macro:ParserInliningReduce case $0$: /* reduce $2$ */ - { - ArrayList list = new$0$(); - push(goTo($1$), list); - } - break; + { + ArrayList list = new$0$(); + push(goTo($1$), list); + } + break; $ Macro:ParserNoInliningReduce case $0$: /* reduce $3$ */ - { - ArrayList list = new$0$(); - push(goTo($1$), list, $2$); - } - break; + { + ArrayList list = new$0$(); + push(goTo($1$), list, $2$); + } + break; $ @@ -385,12 +385,12 @@ $ Macro:ParserNewHeader - + protected ArrayList new$0$() /* reduce $1$ */ { this.firstPopped = null; this.lastPopped = null; - final boolean addElementsToNewList = addElementsFromListToNewList("$1$"); + final boolean addElementsToNewList = addElementsFromListToNewList("$1$"); ArrayList nodeList = new ArrayList(); @@ -456,35 +456,35 @@ Macro:ParserNewBodyNewTail $ Macro:ParserTypedLinkedListAdd - if($2$Node$3$ != null) - { - $0$Node$1$.add($2$Node$3$); - } + if($2$Node$3$ != null) + { + $0$Node$1$.add($2$Node$3$); + } $ Macro:ParserTypedLinkedListAddAll - if($2$Node$3$ != null) //Macro:ParserTypedLinkedListAddAll - { - if(addElementsToNewList){ - $0$Node$1$.addAll($2$Node$3$); - }else{ - $0$Node$1$ = $2$Node$3$; - } - } + if($2$Node$3$ != null) //Macro:ParserTypedLinkedListAddAll + { + if(addElementsToNewList){ + $0$Node$1$.addAll($2$Node$3$); + }else{ + $0$Node$1$ = $2$Node$3$; + } + } $ Macro:ParserTypedLinkedListAddAll2 - if($2$ != null) //Macro:ParserTypedLinkedListAddAll2 - { - $0$Node$1$.addAll($2$); - } + if($2$ != null) //Macro:ParserTypedLinkedListAddAll2 + { + $0$Node$1$.addAll($2$); + } $ Macro:ParserNewBodyListAdd - nodeList.add($0$Node$1$); + nodeList.add($0$Node$1$); $ @@ -493,7 +493,7 @@ Macro:ParserNewTail final ArrayList containerList = nodeList; Object elementToCheck = containerList.get(0); checkResult(elementToCheck); - + return containerList; } @@ -548,7 +548,7 @@ $ Macro:ParserTail - static + static { try { @@ -664,7 +664,7 @@ package $0$; import $1$.*; -@SuppressWarnings("serial") +@SuppressWarnings("serial") public class ParserException extends Exception { Token token; diff --git a/src/main/resources/org/sablecc/sablecc/tokens.txt b/src/main/resources/org/sablecc/sablecc/tokens.txt index 479741c6989ac976952433868833e13eba646ca4..a58680930db48d9e76ac18a4570fce2b9a31b256 100644 --- a/src/main/resources/org/sablecc/sablecc/tokens.txt +++ b/src/main/resources/org/sablecc/sablecc/tokens.txt @@ -15,7 +15,7 @@ package $0$; import $1$.*; - + public final class $2$ extends Token { public $2$(String text) @@ -55,7 +55,7 @@ package $0$; import $1$.*; - + public final class $2$ extends Token { public $2$()