package org.simantics.scl.compiler.parser.grammar.input; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.util.ArrayList; import org.simantics.scl.compiler.parser.grammar.Grammar; import org.simantics.scl.compiler.parser.grammar.Production; import org.simantics.scl.compiler.parser.regexp.RAtom; import org.simantics.scl.compiler.parser.regexp.Regexp; import gnu.trove.list.array.TIntArrayList; import gnu.trove.map.hash.TIntByteHashMap; import gnu.trove.map.hash.TObjectIntHashMap; public class GrammarParserImpl extends GrammarParser { private final GrammarLexer lexer; ArrayList terminals = new ArrayList(); ArrayList nonterminals = new ArrayList(); TObjectIntHashMap symbols = new TObjectIntHashMap(); ArrayList productions = new ArrayList(); TIntArrayList initials = new TIntArrayList(); public GrammarParserImpl(Reader reader) { lexer = new GrammarLexer(reader); } private int getId(String symbol) { if(symbols.contains(symbol)) return symbols.get(symbol); int id; if(Character.isUpperCase(symbol.charAt(0))) { id = terminals.size(); terminals.add(symbol); } else { id = ~nonterminals.size(); nonterminals.add(symbol); } symbols.put(symbol, id); return id; } @Override protected Token nextToken() { try { Token token = lexer.nextToken(); return token; } catch(Exception e) { if(e instanceof RuntimeException) throw (RuntimeException)e; else throw new RuntimeException(e); } } @Override protected RuntimeException syntaxError(Token token, String description) { return new RuntimeException(description); } @Override protected Object reduceFile() { return null; } @Override protected Object reduceProduction() { int lhs = getId(((Token)get(0)).text); for(int i=2;i regexps = new ArrayList(length()); for(int i=0;i