/** Reads a scalar, map, or type name. */ public String readDataType() { String name = readWord(); return readDataType(name); }
/** Reads an integer and returns it. */ public int readInt() { String tag = readWord(); try { int radix = 10; if (tag.startsWith("0x") || tag.startsWith("0X")) { tag = tag.substring("0x".length()); radix = 16; } return Integer.valueOf(tag, radix); } catch (Exception e) { throw unexpected("expected an integer but was " + tag); } }
/** Reads a (paren-wrapped), [square-wrapped] or naked symbol name. */ public String readName() { String optionName; char c = peekChar(); if (c == '(') { pos++; optionName = readWord(); if (readChar() != ')') throw unexpected("expected ')'"); } else if (c == '[') { pos++; optionName = readWord(); if (readChar() != ']') throw unexpected("expected ']'"); } else { optionName = readWord(); } return optionName; }
/** Reads extensions like "extensions 101;" or "extensions 101 to max;". */ private ExtensionsElement readExtensions(Location location, String documentation) { int start = reader.readInt(); // Range start. int end = start; if (reader.peekChar() != ';') { if (!"to".equals(reader.readWord())) throw reader.unexpected("expected ';' or 'to'"); String s = reader.readWord(); // Range end. if (s.equals("max")) { end = Util.MAX_TAG_VALUE; } else { end = Integer.parseInt(s); } } reader.require(';'); return ExtensionsElement.create(location, start, end, documentation); }
/** Reads a quoted or unquoted string and returns it. */ public String readString() { skipWhitespace(true); char c = peekChar(); return c == '"' || c == '\'' ? readQuotedString() : readWord(); }
private void readDeclaration(String documentation) { Location location = reader.location(); String label = reader.readWord(); if (label.equals("package")) { if (packageName != null) throw reader.unexpected(location, "too many package names"); packageName = reader.readName(); reader.require(';'); } else if (label.equals("import")) { String importString = reader.readString(); imports.add(importString); reader.require(';'); } else if (label.equals("type")) { typeConfigs.add(readTypeConfig(location, documentation)); } else { throw reader.unexpected(location, "unexpected label: " + label); } }
/** Reads a reserved tags and names list like "reserved 10, 12 to 14, 'foo';". */ private ReservedElement readReserved(Location location, String documentation) { ImmutableList.Builder<Object> valuesBuilder = ImmutableList.builder(); while (true) { char c = reader.peekChar(); if (c == '"' || c == '\'') { valuesBuilder.add(reader.readQuotedString()); } else { int tagStart = reader.readInt(); c = reader.peekChar(); if (c != ',' && c != ';') { if (!reader.readWord().equals("to")) { throw reader.unexpected("expected ',', ';', or 'to'"); } int tagEnd = reader.readInt(); valuesBuilder.add(Range.closed(tagStart, tagEnd)); } else { valuesBuilder.add(tagStart); } } c = reader.readChar(); if (c == ';') break; if (c != ',') throw reader.unexpected("expected ',' or ';'"); } ImmutableList<Object> values = valuesBuilder.build(); if (values.isEmpty()) { throw reader.unexpected("'reserved' must have at least one field name or tag"); } return ReservedElement.create(location, documentation, values); }
while (!reader.peekChar('}')) { Location wordLocation = reader.location(); String word = reader.readWord(); switch (word) { case "target": if (target != null) throw reader.unexpected(wordLocation, "too many targets"); target = reader.readWord(); if (!reader.readWord().equals("using")) throw reader.unexpected("expected 'using'"); String adapterType = reader.readWord(); reader.require('#'); String adapterConstant = reader.readWord(); reader.require(';'); adapter = adapterType + '#' + adapterConstant;
public ProfileFileElement read() { String label = reader.readWord(); if (!label.equals("syntax")) throw reader.unexpected("expected 'syntax'"); reader.require('='); String syntaxString = reader.readQuotedString(); if (!syntaxString.equals("wire2")) throw reader.unexpected("expected 'wire2'"); reader.require(';'); while (true) { String documentation = reader.readDocumentation(); if (reader.exhausted()) { return fileBuilder.packageName(packageName) .imports(imports.build()) .typeConfigs(typeConfigs.build()) .build(); } readDeclaration(documentation); } }
/** Reads a value that can be a map, list, string, number, boolean or enum. */ private KindAndValue readKindAndValue() { char peeked = reader.peekChar(); switch (peeked) { case '{': return KindAndValue.of(MAP, readMap('{', '}', ':')); case '[': return KindAndValue.of(LIST, readList()); case '"': case '\'': return KindAndValue.of(STRING, reader.readString()); default: if (Character.isDigit(peeked) || peeked == '-') { return KindAndValue.of(NUMBER, reader.readWord()); } String word = reader.readWord(); switch (word) { case "true": return KindAndValue.of(BOOLEAN, "true"); case "false": return KindAndValue.of(BOOLEAN, "false"); default: return KindAndValue.of(ENUM, word); } } }
private GroupElement readGroup(Location location, String documentation, Field.Label label) { String name = reader.readWord(); reader.require('='); int tag = reader.readInt(); GroupElement.Builder builder = GroupElement.builder(location) .label(label) .name(name) .tag(tag) .documentation(documentation); ImmutableList.Builder<FieldElement> fields = ImmutableList.builder(); reader.require('{'); while (true) { String nestedDocumentation = reader.readDocumentation(); if (reader.peekChar('}')) break; Location fieldLocation = reader.location(); String fieldLabel = reader.readWord(); Object field = readField(nestedDocumentation, fieldLocation, fieldLabel); if (!(field instanceof FieldElement)) { throw reader.unexpected("expected field declaration, was " + field); } fields.add((FieldElement) field); } return builder.fields(fields.build()) .build(); }
String label = reader.readWord();
/** Reads a scalar, map, or type name. */ public String readDataType() { String name = readWord(); return readDataType(name); }
/** Reads an integer and returns it. */ public int readInt() { String tag = readWord(); try { int radix = 10; if (tag.startsWith("0x") || tag.startsWith("0X")) { tag = tag.substring("0x".length()); radix = 16; } return Integer.valueOf(tag, radix); } catch (Exception e) { throw unexpected("expected an integer but was " + tag); } }
/** Reads a (paren-wrapped), [square-wrapped] or naked symbol name. */ public String readName() { String optionName; char c = peekChar(); if (c == '(') { pos++; optionName = readWord(); if (readChar() != ')') throw unexpected("expected ')'"); } else if (c == '[') { pos++; optionName = readWord(); if (readChar() != ']') throw unexpected("expected ']'"); } else { optionName = readWord(); } return optionName; }
/** Reads extensions like "extensions 101;" or "extensions 101 to max;". */ private ExtensionsElement readExtensions(Location location, String documentation) { int start = reader.readInt(); // Range start. int end = start; if (reader.peekChar() != ';') { if (!"to".equals(reader.readWord())) throw reader.unexpected("expected ';' or 'to'"); String s = reader.readWord(); // Range end. if (s.equals("max")) { end = Util.MAX_TAG_VALUE; } else { end = Integer.parseInt(s); } } reader.require(';'); return ExtensionsElement.create(location, start, end, documentation); }
/** Reads a quoted or unquoted string and returns it. */ public String readString() { skipWhitespace(true); char c = peekChar(); return c == '"' || c == '\'' ? readQuotedString() : readWord(); }
private void readDeclaration(String documentation) { Location location = reader.location(); String label = reader.readWord(); if ("package".equals(label)) { if (packageName != null) { throw reader.unexpected(location, "too many package names"); } packageName = reader.readName(); reader.require(';'); } else if ("import".equals(label)) { String importString = reader.readString(); imports.add(importString); reader.require(';'); } else if ("type".equals(label)) { typeConfigs.add(readTypeConfig(location, documentation)); } else { throw reader.unexpected(location, "unexpected label: " + label); } }
public AbstractProfileFileElement read() { String label = reader.readWord(); if (!"syntax".equals(label)) { throw reader.unexpected("expected 'syntax'"); } reader.require('='); String syntaxString = reader.readQuotedString(); if (!"wire2".equals(syntaxString)) { throw reader.unexpected("expected 'wire2'"); } reader.require(';'); while (true) { String documentation = reader.readDocumentation(); if (reader.exhausted()) { return fileBuilder.packageName(packageName) .imports(imports.build()) .typeConfigs(typeConfigs.build()) .build(); } readDeclaration(documentation); } }