@Override public Integer symbol( final SExpressionSymbolType ss) throws IOException { w.print(ss.text()); return Integer.valueOf(0); } });
private static void checkKeyword(final SExpressionSymbolType se) throws JPRACompilerParseException { if (!KEYWORDS.contains(se.text())) { final StringBuilder sb = new StringBuilder(256); sb.append("Unrecognized keyword '"); sb.append(se.text()); sb.append("'"); sb.append(System.lineSeparator()); sb.append(" Expected one of: "); sb.append(KEYWORDS); sb.append(System.lineSeparator()); throw JPRACompilerParseException.unrecognizedKeyword(se, sb.toString()); } }
private static void checkPackedFieldKeyword(final SExpressionSymbolType se) throws JPRACompilerParseException { if (!PACKED_FIELD_KEYWORDS.contains(se.text())) { final StringBuilder sb = new StringBuilder(256); sb.append("Unrecognized packed field keyword '"); sb.append(se.text()); sb.append("'"); sb.append(System.lineSeparator()); sb.append(" Expected one of: "); sb.append(PACKED_FIELD_KEYWORDS); sb.append(System.lineSeparator()); throw JPRACompilerParseException.unrecognizedPackedFieldKeyword( se, sb.toString()); } }
private static void checkType(final SExpressionSymbolType se) throws JPRACompilerParseException { if (!TYPES.contains(se.text())) { final StringBuilder sb = new StringBuilder(256); sb.append("Unrecognized type keyword '"); sb.append(se.text()); sb.append("'"); sb.append(System.lineSeparator()); sb.append(" Expected one of: "); sb.append(TYPES); sb.append(System.lineSeparator()); throw JPRACompilerParseException.unrecognizedTypeKeyword( se, sb.toString()); } }
private static void checkIntegerTypeKeyword(final SExpressionSymbolType se) throws JPRACompilerParseException { if (!INTEGER_TYPES.contains(se.text())) { final StringBuilder sb = new StringBuilder(256); sb.append("Unrecognized integer type keyword '"); sb.append(se.text()); sb.append("'"); sb.append(System.lineSeparator()); sb.append(" Expected one of: "); sb.append(INTEGER_TYPES); sb.append(System.lineSeparator()); throw JPRACompilerParseException.unrecognizedIntegerTypeKeyword( se, sb.toString()); } }
private static void checkSizeFunction(final SExpressionSymbolType se) throws JPRACompilerParseException { if (!SIZE_FUNCTIONS.contains(se.text())) { final StringBuilder sb = new StringBuilder(256); sb.append("Unrecognized size function '"); sb.append(se.text()); sb.append("'"); sb.append(System.lineSeparator()); sb.append(" Expected one of: "); sb.append(SIZE_FUNCTIONS); sb.append(System.lineSeparator()); throw JPRACompilerParseException.unrecognizedSizeFunction( se, sb.toString()); } }
private static void checkRecordFieldKeyword(final SExpressionSymbolType se) throws JPRACompilerParseException { if (!RECORD_FIELD_KEYWORDS.contains(se.text())) { final StringBuilder sb = new StringBuilder(256); sb.append("Unrecognized record field keyword '"); sb.append(se.text()); sb.append("'"); sb.append(System.lineSeparator()); sb.append("Expected one of: "); sb.append(RECORD_FIELD_KEYWORDS); sb.append(System.lineSeparator()); throw JPRACompilerParseException.unrecognizedRecordFieldKeyword( se, sb.toString()); } }
@Override public SizeExprType<Unresolved, Untyped> symbol( final SExpressionSymbolType se) throws JPRACompilerParseException { try { return new SizeExprConstant<>( getExpressionLexical(se), new BigInteger(se.text())); } catch (final NumberFormatException x) { throw JPRACompilerParseException.invalidIntegerConstant(se); } } });
/** * @param e The expression * * @return A parser exception * * @see JPRAParseErrorCode#INVALID_INTEGER_CONSTANT */ public static JPRACompilerParseException invalidIntegerConstant( final SExpressionSymbolType e) { final StringBuilder mb = new StringBuilder(256); mb.append(" Expected: An integer constant"); mb.append(System.lineSeparator()); mb.append(" Got: A symbol '"); mb.append(e.text()); mb.append("'"); final String m = NullCheck.notNull(mb.toString(), "Message"); return new JPRACompilerParseException( e.lexical().map(LexicalPosition::copyOf), JPRAParseErrorCode.INVALID_INTEGER_CONSTANT, m); }
/** * @param e The expression * * @return A parser exception * * @see JPRAParseErrorCode#EXPECTED_LIST_GOT_SYMBOL */ public static JPRACompilerParseException expectedListGotSymbol( final SExpressionSymbolType e) { final StringBuilder mb = new StringBuilder(256); mb.append(" Expected: A list"); mb.append(System.lineSeparator()); mb.append(" Got: A symbol '"); mb.append(e.text()); mb.append("'"); final String m = NullCheck.notNull(mb.toString(), "Message"); return new JPRACompilerParseException( e.lexical().map(LexicalPosition::copyOf), JPRAParseErrorCode.EXPECTED_LIST_GOT_SYMBOL, m); }
private StatementPackageEnd<Unresolved, Untyped> parsePackageEnd( final SExpressionListType le, final SExpressionSymbolType se) throws JPRACompilerParseException { Preconditions.checkPreconditionV( Objects.equals(PACKAGE_END, se.text()), "Text must be %s", PACKAGE_END); if (le.size() == 1) { return new StatementPackageEnd<>(getExpressionLexical(se)); } try (final ByteArrayOutputStream bao = new ByteArrayOutputStream(256)) { this.serial.serialize(le, bao); final StringBuilder sb = new StringBuilder(128); sb.append("Syntax error."); sb.append(System.lineSeparator()); sb.append(" Expected: (package-end)"); sb.append(System.lineSeparator()); sb.append(" Got: "); sb.append(bao.toString(StandardCharsets.UTF_8.name())); throw JPRACompilerParseException.syntaxError(le, sb.toString()); } catch (final IOException e) { throw new UnreachableCodeException(e); } }
private StatementCommandSize<Unresolved, Untyped> parseCommandSize( final SExpressionListType le, final SExpressionSymbolType se) throws JPRACompilerParseException { Preconditions.checkPreconditionV( Objects.equals(COMMAND_SIZE, se.text()), "Text must be %s", COMMAND_SIZE); if (le.size() == 2) { return new StatementCommandSize<>(this.parseSizeExpression(le.get(1))); } try (final ByteArrayOutputStream bao = new ByteArrayOutputStream(256)) { this.serial.serialize(le, bao); final StringBuilder sb = new StringBuilder(128); sb.append("Syntax error."); sb.append(System.lineSeparator()); sb.append(" Expected: (:size <size-expression>)"); sb.append(System.lineSeparator()); sb.append(" Got: "); sb.append(bao.toString(StandardCharsets.UTF_8.name())); throw JPRACompilerParseException.syntaxError(le, sb.toString()); } catch (final IOException e) { throw new UnreachableCodeException(e); } }
private StatementCommandType<Unresolved, Untyped> parseCommandType( final SExpressionListType le, final SExpressionSymbolType se) throws JPRACompilerParseException { Preconditions.checkPreconditionV( Objects.equals(COMMAND_TYPE, se.text()), "Text must be %s", COMMAND_TYPE); if (le.size() == 2) { return new StatementCommandType<>(this.parseTypeExpression(le.get(1))); } try (final ByteArrayOutputStream bao = new ByteArrayOutputStream(256)) { this.serial.serialize(le, bao); final StringBuilder sb = new StringBuilder(128); sb.append("Syntax error."); sb.append(System.lineSeparator()); sb.append(" Expected: (:type <type-expression>)"); sb.append(System.lineSeparator()); sb.append(" Got: "); sb.append(bao.toString(StandardCharsets.UTF_8.name())); throw JPRACompilerParseException.syntaxError(le, sb.toString()); } catch (final IOException e) { throw new UnreachableCodeException(e); } }
private SizeExprType<Unresolved, Untyped> parseSizeInBits( final SExpressionListType le, final SExpressionSymbolType se) throws JPRACompilerParseException { Preconditions.checkPreconditionV( Objects.equals(SIZE_IN_BITS, se.text()), "Text must be %s", SIZE_IN_BITS); if (le.size() == 2) { return new SizeExprInBits<>(this.parseTypeExpression(le.get(1))); } try (final ByteArrayOutputStream bao = new ByteArrayOutputStream(256)) { this.serial.serialize(le, bao); final StringBuilder sb = new StringBuilder(128); sb.append("Syntax error."); sb.append(System.lineSeparator()); sb.append(" Expected: (size-in-bits <type-expression>)"); sb.append(System.lineSeparator()); sb.append(" Got: "); sb.append(bao.toString(StandardCharsets.UTF_8.name())); throw JPRACompilerParseException.syntaxError(le, sb.toString()); } catch (final IOException e) { throw new UnreachableCodeException(e); } }
private SizeExprType<Unresolved, Untyped> parseSizeInOctets( final SExpressionListType le, final SExpressionSymbolType se) throws JPRACompilerParseException { Preconditions.checkPreconditionV( Objects.equals(SIZE_IN_OCTETS, se.text()), "Text must be %s", SIZE_IN_OCTETS); if (le.size() == 2) { return new SizeExprInOctets<>(this.parseTypeExpression(le.get(1))); } try (final ByteArrayOutputStream bao = new ByteArrayOutputStream(256)) { this.serial.serialize(le, bao); final StringBuilder sb = new StringBuilder(128); sb.append("Syntax error."); sb.append(System.lineSeparator()); sb.append(" Expected: (size-in-octets <type-expression>)"); sb.append(System.lineSeparator()); sb.append(" Got: "); sb.append(bao.toString(StandardCharsets.UTF_8.name())); throw JPRACompilerParseException.syntaxError(le, sb.toString()); } catch (final IOException e) { throw new UnreachableCodeException(e); } }
private static TypeName parseTypeName(final SExpressionSymbolType name) throws JPRACompilerParseException { try { return new TypeName( getExpressionLexical(name), name.text()); } catch (final PreconditionViolationException e) { throw JPRACompilerParseException.badTypeName(name, e.getMessage()); } }
private static FieldName parseFieldName(final SExpressionSymbolType name) throws JPRACompilerParseException { try { return new FieldName( getExpressionLexical(name), name.text()); } catch (final PreconditionViolationException e) { throw JPRACompilerParseException.badFieldName(name, e.getMessage()); } }
private static PackageNameUnqualified parsePackageNameUnqualified( final SExpressionSymbolType s) throws JPRACompilerParseException { try { return new PackageNameUnqualified( getExpressionLexical(s), s.text()); } catch (final PreconditionViolationException e) { throw JPRACompilerParseException.badPackageName(s, e.getMessage()); } }
@Override public SizeExprType<Unresolved, Untyped> list( final SExpressionListType le) throws JPRACompilerParseException { if (le.size() == 0) { throw JPRACompilerParseException.expectedNonEmptyList(le); } final SExpressionSymbolType se = requireSymbol(le.get(0)); checkSizeFunction(se); switch (se.text()) { case SIZE_IN_BITS: { return JPRAParser.this.parseSizeInBits(le, se); } case SIZE_IN_OCTETS: { return JPRAParser.this.parseSizeInOctets(le, se); } default: throw new UnreachableCodeException(); } }
private static PackageNameQualified parsePackageNameQualified( final SExpressionSymbolType name) throws JPRACompilerParseException { final String text = name.text(); Preconditions.checkPrecondition(!text.isEmpty(), "Text must not be empty"); final String[] segments = text.split("\\."); final Optional<LexicalPosition<Path>> ilex = getExpressionLexical(name); final MutableList<PackageNameUnqualified> names_base = new FastList<>(); for (int index = 0; index < segments.length; ++index) { final String raw = segments[index]; try { names_base.add(new PackageNameUnqualified(ilex, raw)); } catch (final PreconditionViolationException e) { throw JPRACompilerParseException.badPackageName(name, e.getMessage()); } } final ImmutableList<PackageNameUnqualified> names = names_base.toImmutable(); return new PackageNameQualified(names); }