public static Package parseFile(String filename) throws FileNotFoundException, IOException, EGraphQLException, EGraphEngine { String src = TextFile.fileToString(filename); return parse(src); }
public static Package parse(String source) throws IOException, EGraphQLException, EGraphEngine { Parser self = new Parser(); self.reader = new StringReader(source); self.next(); Document doc = self.parseDocument(); return new Package(doc); }
private Field parseField() throws EGraphQLException, IOException { Field result = new Field(); result.setName(consumeName()); result.setAlias(result.getName()); if (hasPunctuation(":")) { consumePunctuation(":"); result.setName(consumeName()); } if (hasPunctuation("(")) { consumePunctuation("("); while (!hasPunctuation(")")) result.getArguments().add(parseArgument()); consumePunctuation(")"); } while (hasPunctuation("@")) result.getDirectives().add(parseDirective()); if (hasPunctuation("{")) { consumePunctuation("{"); do { result.getSelectionSet().add(parseSelection()); } while (!hasPunctuation("}")); consumePunctuation("}"); } return result; }
private void skipIgnore() throws IOException{ char ch = getNextChar(); while (Character.isWhitespace(ch) || (ch == ',')) ch = getNextChar(); if (ch == '#') { while (ch != '\r' && ch != '\n') ch = getNextChar(); pushChar(ch); skipIgnore(); } else pushChar(ch); }
private void parseFragmentInner(Fragment fragment) throws EGraphQLException, IOException { while (hasPunctuation("@")) fragment.getDirectives().add(parseDirective()); consumePunctuation("{"); do fragment.getSelectionSet().add(parseSelection()); while (!hasPunctuation("}")); consumePunctuation("}"); }
private Selection parseSelection() throws EGraphQLException, IOException { Selection result = new Selection(); if (hasPunctuation("...")) { consumePunctuation("..."); if (hasName() && !token.toString().equals("on")) result.setFragmentSpread(parseFragmentSpread()); else result.setInlineFragment(parseInlineFragment()); } else result.setField(parseField()); return result; }
private Variable parseVariable() throws EGraphQLException, IOException { Variable result = new Variable(); consumePunctuation("$"); result.setName(consumeName()); consumePunctuation(":"); result.setTypeName(consumeName()); if (hasPunctuation("=")) { consumePunctuation("="); result.setDefaultValue(parseValue()); } return result; }
private Document parseDocument() throws EGraphQLException, IOException, EGraphEngine { Document doc = new Document(); if (!hasName()) { Operation op = new Operation(); parseOperationInner(op); doc.getOperations().add(op); } else { while (!readerDone || (peek != null)) { String s = consumeName(); if (s.equals("mutation") || (s.equals("query"))) doc.getOperations().add(parseOperation(s)); else if (s.equals("fragment")) doc.getFragments().add(parseFragment()); else throw new EGraphEngine("Not done yet"); // doc.Operations.Add(parseOperation(s))? } } return doc; }
private Directive parseDirective() throws EGraphQLException, IOException { Directive result = new Directive(); consumePunctuation("@"); result.setName(consumeName()); if (hasPunctuation("(")) { consumePunctuation("("); do { result.getArguments().add(parseArgument()); } while (!hasPunctuation(")")); consumePunctuation(")"); } return result; }
private Value parseValue() throws EGraphQLException, IOException { Value result = null; switch (lexType) { case gqlltNull: throw new EGraphQLException("Attempt to read a value after reading off the } of the GraphQL statement"); case gqlltName: result = new NameValue(token.toString()); break; case gqlltPunctuation: if (hasPunctuation("$")) { consumePunctuation("$"); result = new VariableValue(token.toString()); } else if (hasPunctuation("{")) { consumePunctuation("{"); ObjectValue obj = new ObjectValue(); while (!hasPunctuation("}")) obj.getFields().add(parseArgument()); result = obj; } else throw new EGraphQLException("Attempt to read a value at \""+token.toString()+"\""); break; case gqlltString: result = new StringValue(token.toString()); break; case gqlltNumber: result = new NumberValue(token.toString()); break; } next(); return result; }
private FragmentSpread parseFragmentSpread() throws EGraphQLException, IOException { FragmentSpread result = new FragmentSpread(); result.setName(consumeName()); while (hasPunctuation("@")) result.getDirectives().add(parseDirective()); return result; }
private Fragment parseInlineFragment() throws EGraphQLException, IOException { Fragment result = new Fragment(); if (hasName("on")) { consumeName("on"); result.setTypeCondition(consumeName()); } parseFragmentInner(result); return result; }
private Operation parseOperation(String name) throws EGraphQLException, IOException { Operation result = new Operation(); if (name.equals("mutation")) { result.setOperationType(Operation.OperationType.qglotMutation); if (hasName()) result.setName(consumeName()); } else if (name.equals("query")) { result.setOperationType(Operation.OperationType.qglotQuery); if (hasName()) result.setName(consumeName()); } else result.setName(name); parseOperationInner(result); return result; }
private Fragment parseFragment() throws EGraphQLException, IOException { Fragment result = new Fragment(); result.setName(consumeName()); consumeName("on"); result.setTypeCondition(consumeName()); parseFragmentInner(result); return result; }
private String consumeName() throws EGraphQLException, IOException { if (lexType != LexType.gqlltName) throw new EGraphQLException("Found \""+token.toString()+"\" expecting a name"); String result = token.toString(); next(); return result; }
private void parseOperationInner(Operation op) throws EGraphQLException, IOException { if (hasPunctuation("(")) { consumePunctuation("("); do op.getVariables().add(parseVariable()); while (!hasPunctuation(")")); consumePunctuation(")"); } while (hasPunctuation("@")) op.getDirectives().add(parseDirective()); if (hasPunctuation("{")) { consumePunctuation("{"); do op.getSelectionSet().add(parseSelection()); while (!hasPunctuation("}")); consumePunctuation("}"); } }
private Selection parseSelection() throws EGraphQLException, IOException { Selection result = new Selection(); if (hasPunctuation("...")) { consumePunctuation("..."); if (hasName() && !token.toString().equals("on")) result.setFragmentSpread(parseFragmentSpread()); else result.setInlineFragment(parseInlineFragment()); } else result.setField(parseField()); return result; }
private Argument parseArgument() throws EGraphQLException, IOException { Argument result = new Argument(); result.setName(consumeName()); consumePunctuation(":"); if (hasPunctuation("[")) { result.setListStatus(ArgumentListStatus.REPEATING); consumePunctuation("["); while (!hasPunctuation("]")) result.getValues().add(parseValue()); consumePunctuation("]"); } else result.getValues().add(parseValue()); return result; }
private Document parseDocument() throws EGraphQLException, IOException, EGraphEngine { Document doc = new Document(); if (!hasName()) { Operation op = new Operation(); parseOperationInner(op); doc.getOperations().add(op); } else { while (!readerDone || (peek != null)) { String s = consumeName(); if (s.equals("mutation") || (s.equals("query"))) doc.getOperations().add(parseOperation(s)); else if (s.equals("fragment")) doc.getFragments().add(parseFragment()); else throw new EGraphEngine("Not done yet"); // doc.Operations.Add(parseOperation(s))? } } return doc; }
skipIgnore(); token = new StringBuilder(); if (readerDone && peek == null) lexType = LexType.gqlltNull; else { char ch = getNextChar(); if (Utilities.existsInList(ch, '!', '$', '(', ')', ':', '=', '@', '[', ']', '{', '|', '}')) { lexType = LexType.gqlltPunctuation; do { token.append(ch); ch = getNextChar(); } while (ch == '.'); pushChar(ch); if ((token.length() != 3)) throw new EGraphQLException("Found \""+token.toString()+"\" expecting \"...\""); do { token.append(ch); ch = getNextChar(); } while ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9') || (ch == '_')); pushChar(ch); } else if ((ch >= '0' && ch <= '9') || (ch == '-')) { lexType = LexType.gqlltNumber; do { token.append(ch); ch = getNextChar(); } while ((ch >= '0' && ch <= '9') || ((ch == '.') && token.toString().indexOf('.') == -1) || ((ch == 'e') && token.toString().indexOf('e') == -1)); pushChar(ch);