Search in sources :

Example 11 with TokenizerException

use of net.morimekta.providence.serializer.pretty.TokenizerException in project providence by morimekta.

the class ProvidenceConfigContext method initReference.

String initReference(Token token, Tokenizer tokenizer) throws TokenizerException {
    String reference = token.asString();
    if (ProvidenceConfigUtil.RESERVED_WORDS.contains(reference)) {
        throw new TokenizerException(token, "Trying to assign reference id '%s', which is reserved.", reference).setLine(tokenizer.getLine());
    }
    TokenizerException ex = referenceExceptions.get(reference);
    if (ex != null) {
        if (references.containsKey(reference)) {
            throw new TokenizerException(token, "Trying to reassign reference '%s', original at line %d", reference, ex.getLineNo()).setLine(tokenizer.getLine()).initCause(ex);
        }
        throw new TokenizerException(token, "Trying to reassign reference '%s' while calculating it's value, original at line %d", reference, ex.getLineNo()).setLine(tokenizer.getLine()).initCause(ex);
    } else if (includeAliases.contains(reference)) {
        throw new TokenizerException(token, "Trying to reassign include alias '%s' to reference.", reference).setLine(tokenizer.getLine());
    }
    referenceExceptions.put(reference, new TokenizerException(token, "Original reference").setLine(tokenizer.getLine()));
    return reference;
}
Also used : TokenizerException(net.morimekta.providence.serializer.pretty.TokenizerException)

Example 12 with TokenizerException

use of net.morimekta.providence.serializer.pretty.TokenizerException in project providence by morimekta.

the class Config method run.

public void run(String... args) {
    ConfigOptions op = new ConfigOptions(tty);
    try {
        ArgumentParser cli = op.getArgumentParser("pvdcfg", "Providence Config Tool");
        try {
            cli.parse(args);
            if (op.showHelp()) {
                System.out.println(cli.getDescription() + " - " + getVersionString());
                System.out.println("Usage: " + cli.getSingleLineUsage());
                System.out.println();
                cli.printUsage(System.out);
                System.out.println();
                System.out.println("Available Commands:");
                System.out.println();
                op.getCommandSet().printUsage(System.out);
                return;
            } else if (op.showVersion()) {
                System.out.println(cli.getDescription() + " - " + getVersionString());
                return;
            }
            cli.validate();
            op.execute();
            return;
        } catch (ArgumentException e) {
            System.err.println("Invalid argument: " + e.getMessage());
            System.err.println("Usage: " + cli.getSingleLineUsage());
            if (op.verbose()) {
                e.printStackTrace();
            }
        } catch (ParseException e) {
            System.out.flush();
            System.err.println(e.asString());
            if (op.verbose()) {
                System.err.println();
                e.printStackTrace();
            }
        } catch (TokenizerException e) {
            System.out.flush();
            System.err.println(e.asString());
            if (op.verbose()) {
                System.err.println();
                e.printStackTrace();
            }
        } catch (SerializerException e) {
            System.out.flush();
            System.err.println("Serialization error: " + e.toString());
            if (op.verbose()) {
                System.err.println();
                e.printStackTrace();
            }
        } catch (IOException | RuntimeException e) {
            System.out.flush();
            System.err.println("IO Error: " + e.toString());
            if (op.verbose()) {
                System.err.println();
                e.printStackTrace();
            }
        }
    } catch (Exception e) {
        System.out.flush();
        System.err.println("Unhandled exception: " + e.toString());
        if (op.verbose()) {
            System.err.println();
            e.printStackTrace();
        }
    }
    exit(1);
}
Also used : TokenizerException(net.morimekta.providence.serializer.pretty.TokenizerException) ArgumentException(net.morimekta.console.args.ArgumentException) ParseException(net.morimekta.providence.reflect.parser.ParseException) IOException(java.io.IOException) ArgumentParser(net.morimekta.console.args.ArgumentParser) SerializerException(net.morimekta.providence.serializer.SerializerException) TokenizerException(net.morimekta.providence.serializer.pretty.TokenizerException) IOException(java.io.IOException) ParseException(net.morimekta.providence.reflect.parser.ParseException) SerializerException(net.morimekta.providence.serializer.SerializerException) ArgumentException(net.morimekta.console.args.ArgumentException)

Example 13 with TokenizerException

use of net.morimekta.providence.serializer.pretty.TokenizerException in project providence by morimekta.

the class PrettySerializer method readMessage.

private <Message extends PMessage<Message, Field>, Field extends PField> Message readMessage(Tokenizer tokenizer, PMessageDescriptor<Message, Field> descriptor, boolean params) throws IOException {
    PMessageBuilder<Message, Field> builder = descriptor.builder();
    Token token = tokenizer.expect("message field or end");
    for (; ; ) {
        if (params) {
            if (token.isSymbol(Token.kParamsEnd)) {
                break;
            }
        } else if (token.isSymbol(Token.kMessageEnd)) {
            break;
        }
        if (!token.isIdentifier()) {
            throw new TokenizerException(token, "Expected field name, got '%s'", Strings.escape(token.asString())).setLine(tokenizer.getLine());
        }
        tokenizer.expectSymbol("field value separator", Token.kFieldValueSep);
        PField field = descriptor.findFieldByName(token.asString());
        if (field == null) {
            consumeValue(tokenizer, tokenizer.expect("field value"));
        } else {
            builder.set(field.getId(), readFieldValue(tokenizer, tokenizer.expect("field value"), field.getDescriptor()));
        }
        token = tokenizer.expect("Message field or end");
        if (token.isSymbol(Token.kLineSep1) || token.isSymbol(Token.kLineSep2)) {
            token = tokenizer.expect("Message field or end");
        }
    }
    return builder.build();
}
Also used : PField(net.morimekta.providence.descriptor.PField) PMessage(net.morimekta.providence.PMessage) PField(net.morimekta.providence.descriptor.PField) Token(net.morimekta.providence.serializer.pretty.Token) TokenizerException(net.morimekta.providence.serializer.pretty.TokenizerException)

Example 14 with TokenizerException

use of net.morimekta.providence.serializer.pretty.TokenizerException in project providence by morimekta.

the class PrettySerializer method consumeValue.

private void consumeValue(Tokenizer tokenizer, Token token) throws IOException {
    if (token.isSymbol(Token.kMessageStart)) {
        // message or map.
        token = tokenizer.expect("map or message first entry");
        // ignore empty map or message.
        if (!token.isSymbol(Token.kMessageEnd)) {
            // key = value: message
            // key : value: map
            // potential message.
            boolean idKey = token.isIdentifier();
            consumeValue(tokenizer, token);
            if (tokenizer.expectSymbol("map", ':', '=') == Token.kFieldValueSep) {
                // message!
                if (!idKey) {
                // TODO: fail!
                }
                consumeValue(tokenizer, tokenizer.expect("message field value"));
                token = nextNotLineSep(tokenizer);
                while (!token.isSymbol(Token.kMessageEnd)) {
                    if (!token.isIdentifier()) {
                    // TODO: fail.
                    }
                    tokenizer.expectSymbol("message field value sep", Token.kFieldValueSep);
                    consumeValue(tokenizer, tokenizer.expect("message field value"));
                    token = nextNotLineSep(tokenizer);
                }
            } else {
                // map!
                consumeValue(tokenizer, tokenizer.expect("map entry value"));
                token = nextNotLineSep(tokenizer);
                while (!token.isSymbol(Token.kMessageEnd)) {
                    consumeValue(tokenizer, token);
                    tokenizer.expectSymbol("message field value sep", Token.kKeyValueSep);
                    consumeValue(tokenizer, tokenizer.expect("message field value"));
                    token = nextNotLineSep(tokenizer);
                }
            }
            if (!token.isSymbol(Token.kFieldValueSep)) {
                // assume map.
                while (!token.isSymbol(Token.kMessageEnd)) {
                    consumeValue(tokenizer, token);
                    tokenizer.expectSymbol("key value sep.", Token.kKeyValueSep);
                    consumeValue(tokenizer, tokenizer.expect("map value"));
                    // maps do *not* require separator, but allows ',' separator, and separator after last.
                    token = nextNotLineSep(tokenizer);
                }
            } else {
                // assume message.
                while (!token.isSymbol(Token.kMessageEnd)) {
                    if (!token.isIdentifier()) {
                        throw new TokenizerException(token, "Invalid field name: " + token.asString()).setLine(tokenizer.getLine());
                    }
                    tokenizer.expectSymbol("field value sep.", Token.kFieldValueSep);
                    consumeValue(tokenizer, tokenizer.next());
                    token = nextNotLineSep(tokenizer);
                }
            }
        }
    } else if (token.isSymbol(Token.kListStart)) {
        token = tokenizer.expect("");
        while (!token.isSymbol(Token.kListEnd)) {
            consumeValue(tokenizer, token);
            // lists and sets require list separator (,), and allows trailing separator.
            if (tokenizer.expectSymbol("list separator or end", Token.kLineSep1, Token.kListEnd) == Token.kListEnd) {
                break;
            }
            token = tokenizer.expect("list value or end");
        }
    } else if (token.strEquals(Token.HEX) || token.strEquals(Token.B64)) {
        tokenizer.expectSymbol("hex body start", Token.kParamsStart);
        tokenizer.readBinary(Token.kParamsEnd);
    } else if (!(// number (double)
    token.isReal() || // number (int)
    token.isInteger() || // string literal
    token.isStringLiteral() || token.isIdentifier())) {
        // enum value reference.
        throw new TokenizerException(token, "Unknown value token '%s'", token.asString()).setLine(tokenizer.getLine());
    }
}
Also used : TokenizerException(net.morimekta.providence.serializer.pretty.TokenizerException)

Example 15 with TokenizerException

use of net.morimekta.providence.serializer.pretty.TokenizerException in project providence by morimekta.

the class PrettySerializer method deserialize.

@Nonnull
@Override
@SuppressWarnings("unchecked")
public <Message extends PMessage<Message, Field>, Field extends PField> PServiceCall<Message, Field> deserialize(@Nonnull InputStream input, @Nonnull PService service) throws IOException {
    String methodName = null;
    int sequence = 0;
    PServiceCallType callType = null;
    try {
        // pretty printed service calls cannot be chained-serialized, so this should be totally safe.
        Tokenizer tokenizer = new Tokenizer(input);
        Token token = tokenizer.expect("Sequence or type");
        if (token.isInteger()) {
            sequence = (int) token.parseInteger();
            tokenizer.expectSymbol("Sequence type sep", Token.kKeyValueSep);
            token = tokenizer.expectIdentifier("Call Type");
        }
        callType = PServiceCallType.findByName(token.asString().toUpperCase(Locale.US));
        if (callType == null) {
            throw new TokenizerException(token, "No such call type %s", token.asString()).setLine(tokenizer.getLine()).setExceptionType(PApplicationExceptionType.INVALID_MESSAGE_TYPE);
        }
        token = tokenizer.expectIdentifier("method name");
        methodName = token.asString();
        PServiceMethod method = service.getMethod(methodName);
        if (method == null) {
            throw new TokenizerException(token, "no such method %s on service %s", methodName, service.getQualifiedName()).setLine(tokenizer.getLine()).setExceptionType(PApplicationExceptionType.UNKNOWN_METHOD);
        }
        tokenizer.expectSymbol("call params start", Token.kParamsStart);
        Message message;
        switch(callType) {
            case CALL:
            case ONEWAY:
                message = (Message) readMessage(tokenizer, method.getRequestType(), true);
                break;
            case REPLY:
                message = (Message) readMessage(tokenizer, method.getResponseType(), true);
                break;
            case EXCEPTION:
                message = (Message) readMessage(tokenizer, PApplicationException.kDescriptor, true);
                break;
            default:
                throw new IllegalStateException("Unreachable code reached");
        }
        return new PServiceCall<>(methodName, callType, sequence, message);
    } catch (TokenizerException e) {
        e.setCallType(callType).setSequenceNo(sequence).setMethodName(methodName);
        throw e;
    } catch (IOException e) {
        throw new SerializerException(e, e.getMessage()).setCallType(callType).setSequenceNo(sequence).setMethodName(methodName);
    }
}
Also used : PMessage(net.morimekta.providence.PMessage) PServiceCallType(net.morimekta.providence.PServiceCallType) PServiceCall(net.morimekta.providence.PServiceCall) Token(net.morimekta.providence.serializer.pretty.Token) TokenizerException(net.morimekta.providence.serializer.pretty.TokenizerException) IOException(java.io.IOException) Tokenizer(net.morimekta.providence.serializer.pretty.Tokenizer) PServiceMethod(net.morimekta.providence.descriptor.PServiceMethod) Nonnull(javax.annotation.Nonnull)

Aggregations

TokenizerException (net.morimekta.providence.serializer.pretty.TokenizerException)17 Token (net.morimekta.providence.serializer.pretty.Token)7 PMessage (net.morimekta.providence.PMessage)6 IOException (java.io.IOException)5 ProvidenceConfigException (net.morimekta.providence.config.ProvidenceConfigException)4 PMessageDescriptor (net.morimekta.providence.descriptor.PMessageDescriptor)3 BufferedInputStream (java.io.BufferedInputStream)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 PEnumValue (net.morimekta.providence.PEnumValue)2 PMessageBuilder (net.morimekta.providence.PMessageBuilder)2 PEnumDescriptor (net.morimekta.providence.descriptor.PEnumDescriptor)2 PMap (net.morimekta.providence.descriptor.PMap)2 Tokenizer (net.morimekta.providence.serializer.pretty.Tokenizer)2 FileNotFoundException (java.io.FileNotFoundException)1 InputStream (java.io.InputStream)1