@Override public Integer list( final SExpressionListType xs) throws IOException { if (xs.isSquare()) { w.print("["); } else { w.print("("); } final int max = xs.size(); for (int index = 0; index < max; ++index) { final SExpressionType es = xs.get(index); JSXSerializerTrivial.serializeWithWriter(es, w); if ((index + 1) < max) { w.print(" "); } } if (xs.isSquare()) { w.print("]"); } else { w.print(")"); } return Integer.valueOf(0); }
/** * @param e The expression * * @return A parser exception * * @see JPRAParseErrorCode#EXPECTED_NON_EMPTY_LIST */ public static JPRACompilerParseException expectedNonEmptyList( final SExpressionListType e) { Preconditions.checkPrecondition(e.size() == 0, "Size must be zero"); final StringBuilder mb = new StringBuilder(256); mb.append(" Expected: A non-empty list"); mb.append(System.lineSeparator()); mb.append(" Got: An empty list"); final String m = NullCheck.notNull(mb.toString(), "Message"); return new JPRACompilerParseException( e.lexical().map(LexicalPosition::copyOf), JPRAParseErrorCode.EXPECTED_NON_EMPTY_LIST, m); }
@Override public Integer list( final SExpressionListType xs) throws IOException { if (xs.isSquare()) { w.print("["); } else { w.print("("); } final int max = xs.size(); for (int index = 0; index < max; ++index) { final SExpressionType es = xs.get(index); serializeWithWriter(es, w); if ((index + 1) < max) { w.print(" "); } } if (xs.isSquare()) { w.print("]"); } else { w.print(")"); } return Integer.valueOf(0); }
/** * @param e The expression * * @return A parser exception * * @see JPRAParseErrorCode#EXPECTED_NON_EMPTY_LIST */ public static JPRACompilerParseException expectedNonEmptyList( final SExpressionListType e) { Assertive.require(e.size() == 0); final StringBuilder mb = new StringBuilder(256); mb.append(" Expected: A non-empty list"); mb.append(System.lineSeparator()); mb.append(" Got: An empty list"); final String m = NullCheck.notNull(mb.toString()); return new JPRACompilerParseException( e.getLexicalInformation().map(ImmutableLexicalPosition::newFrom), JPRAParseErrorCode.EXPECTED_NON_EMPTY_LIST, m); }
private StatementPackageEnd<Unresolved, Untyped> parsePackageEnd( final SExpressionListType le, final SExpressionSymbolType se) throws JPRACompilerParseException { Assertive.require(JPRAParser.PACKAGE_END.equals(se.getText())); if (le.size() == 1) { return new StatementPackageEnd<>(JPRAParser.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 SizeExprType<Unresolved, Untyped> parseSizeInBits( final SExpressionListType le, final SExpressionSymbolType se) throws JPRACompilerParseException { Assertive.require(JPRAParser.SIZE_IN_BITS.equals(se.getText())); 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 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 SizeExprType<Unresolved, Untyped> parseSizeInOctets( final SExpressionListType le, final SExpressionSymbolType se) throws JPRACompilerParseException { Assertive.require(JPRAParser.SIZE_IN_OCTETS.equals(se.getText())); 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 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 StatementCommandType<Unresolved, Untyped> parseCommandType( final SExpressionListType le, final SExpressionSymbolType se) throws JPRACompilerParseException { Assertive.require(JPRAParser.COMMAND_TYPE.equals(se.getText())); 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 StatementCommandSize<Unresolved, Untyped> parseCommandSize( final SExpressionListType le, final SExpressionSymbolType se) throws JPRACompilerParseException { Assertive.require(JPRAParser.COMMAND_SIZE.equals(se.getText())); 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 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 TypeExprType<Unresolved, Untyped> parseTypeFloat( final SExpressionListType le, final SExpressionSymbolType se) throws JPRACompilerParseException { Assertive.require(JPRAParser.FLOAT.equals(se.getText())); if (le.size() == 2) { final SExpressionType s_expr = le.get(1); return new TypeExprFloat<>( Untyped.get(), JPRAParser.getExpressionLexical(s_expr), this.parseSizeExpression(s_expr)); } 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: (float <size-in-bits>)"); 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 TypeExprType<Unresolved, Untyped> parseTypeFloat( final SExpressionListType le, final SExpressionSymbolType se) throws JPRACompilerParseException { Preconditions.checkPreconditionV( Objects.equals(FLOAT, se.text()), "Text must be %s", FLOAT); if (le.size() == 2) { final SExpressionType s_expr = le.get(1); return new TypeExprFloat<>( Untyped.get(), getExpressionLexical(s_expr), this.parseSizeExpression(s_expr)); } 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: (float <size-in-bits>)"); 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); } }
@Override public SizeExprType<Unresolved, Untyped> list( final SExpressionListType le) throws JPRACompilerParseException { if (le.size() == 0) { throw JPRACompilerParseException.expectedNonEmptyList(le); } final SExpressionSymbolType se = JPRAParser.requireSymbol(le.get(0)); JPRAParser.checkSizeFunction(se); switch (se.getText()) { case JPRAParser.SIZE_IN_BITS: { return JPRAParser.this.parseSizeInBits(le, se); } case JPRAParser.SIZE_IN_OCTETS: { return JPRAParser.this.parseSizeInOctets(le, se); } } throw new UnreachableCodeException(); }
@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(); } }
for (int index = 0; index < e.size(); ++index) { xs = xs.append(receiver.apply(e.get(index)));
@Override public StatementType<Unresolved, Untyped> parseStatement( final SExpressionType expr) throws JPRACompilerParseException { NullCheck.notNull(expr); final SExpressionListType le = JPRAParser.requireList(expr); if (le.size() == 0) { throw JPRACompilerParseException.expectedNonEmptyList(le); } final SExpressionSymbolType se = JPRAParser.requireSymbol(le.get(0)); JPRAParser.checkKeyword(se); switch (se.getText()) { case JPRAParser.PACKAGE_BEGIN: return this.parsePackageBegin(le, se); case JPRAParser.PACKAGE_END: return this.parsePackageEnd(le, se); case JPRAParser.IMPORT: return this.parsePackageImport(le, se); case JPRAParser.RECORD: return this.parseRecord(le, se); case JPRAParser.PACKED: return this.parsePacked(le, se); case JPRAParser.COMMAND_SIZE: return this.parseCommandSize(le, se); case JPRAParser.COMMAND_TYPE: return this.parseCommandType(le, se); } throw new UnreachableCodeException(); }