src/main/java/org/semgus/java/event/EventParser.java
private static SpecEvent parseSynthFun(JSONObject eventDto) throws DeserializationException {
        String name = JsonUtils.getString(eventDto, "name");
        String termType = JsonUtils.getString(eventDto, "termType");

        // parse target grammar specification
        JSONObject grammarDto = JsonUtils.getObject(eventDto, "grammar");
        Map<String, SynthFunEvent.NonTerminal> grammar = new HashMap<>();
        try {
            List<JSONObject> nonTerminalsDto = JsonUtils.getObjects(grammarDto, "nonTerminals");
            List<JSONObject> productionsDto = JsonUtils.getObjects(grammarDto, "productions");

            // construct non-terminals
            for (int i = 0; i < nonTerminalsDto.size(); i++) {
                JSONObject ntDto = nonTerminalsDto.get(i);
                try {
                    String ntName = JsonUtils.getString(ntDto, "name");
                    if (grammar.containsKey(ntName)) {
                        throw new DeserializationException(
                                String.format("Duplicate nonterminal declaration \"%s\"", ntName), "name");
                    }
                    grammar.put(ntName,
                            new SynthFunEvent.NonTerminal(JsonUtils.getString(ntDto, "termType"), new HashMap<>()));
                } catch (DeserializationException e) {
                    throw e.prepend("nonTerminals." + i);
                }
            }

            // construct productions and attach them to their associated non-terminals
            for (int i = 0; i < productionsDto.size(); i++) {
                JSONObject prodDto = productionsDto.get(i);
                try {
                    String ntName = JsonUtils.getString(prodDto, "instance");
                    String operator = JsonUtils.getString(prodDto, "operator");
                    List<String> occurrences = JsonUtils.getStrings(prodDto, "occurrences");

                    SynthFunEvent.NonTerminal nonTerminal = grammar.get(ntName);
                    if (nonTerminal == null) { // ensure non-terminal exists
                        throw new DeserializationException(
                                String.format("Unknown nonterminal \"%s\" referenced in production", ntName),
                                "instance");
                    }
                    if (nonTerminal.productions().containsKey(operator)) { // ensure this production is distinct
                        throw new DeserializationException(
                                String.format("Duplicate production \"%s\" for nonterminal \"%s\"", operator, ntName),
                                "operator");
                    }

                    for (int j = 0; j < occurrences.size(); j++) { // ensure child term non-terminals exist
                        if (!grammar.containsKey(occurrences.get(j))) {
                            throw new DeserializationException(
                                    String.format("Unknown nonterminal \"%s\" referenced in production child", ntName),
                                    "occurrences." + j);
                        }
                    }

                    nonTerminal.productions().put(operator, new SynthFunEvent.Production(operator, occurrences));
                } catch (DeserializationException e) {
                    throw e.prepend("productions." + i);
                }
            }
        } catch (DeserializationException e) {
            throw e.prepend("grammar");
        }

        return new SynthFunEvent(name, grammar, termType);
    }
/**
     * Deserializes a "synth-fun" event.
     *
     * @param eventDto The JSON representation of the event.
     * @return The deserialized event.
     * @throws DeserializationException If {@code eventDto} is not a valid representation of a "synth-fun" event.
     */
Deserializes a "synth-fun" event.
@param eventDto The JSON representation of the event.
@return The deserialized event.
@throws DeserializationException If  eventDto is not a valid representation of a "synth-fun" event.
Deserializes a " synth - fun " event . @param eventDto The JSON representation of the event . @return The deserialized event . @throws DeserializationException If eventDto is not a valid representation of a " synth - fun " event .
private static SpecEvent parseSynthFun(JSONObject eventDto) throws DeserializationException {
        String name = JsonUtils.getString(eventDto, "name");
        String termType = JsonUtils.getString(eventDto, "termType");
        JSONObject grammarDto = JsonUtils.getObject(eventDto, "grammar");
        Map<String, SynthFunEvent.NonTerminal> grammar = new HashMap<>();
        try {
            List<JSONObject> nonTerminalsDto = JsonUtils.getObjects(grammarDto, "nonTerminals");
            List<JSONObject> productionsDto = JsonUtils.getObjects(grammarDto, "productions");
            for (int i = 0; i < nonTerminalsDto.size(); i++) {
                JSONObject ntDto = nonTerminalsDto.get(i);
                try {
                    String ntName = JsonUtils.getString(ntDto, "name");
                    if (grammar.containsKey(ntName)) {
                        throw new DeserializationException(
                                String.format("Duplicate nonterminal declaration \"%s\"", ntName), "name");
                    }
                    grammar.put(ntName,
                            new SynthFunEvent.NonTerminal(JsonUtils.getString(ntDto, "termType"), new HashMap<>()));
                } catch (DeserializationException e) {
                    throw e.prepend("nonTerminals." + i);
                }
            }
            for (int i = 0; i < productionsDto.size(); i++) {
                JSONObject prodDto = productionsDto.get(i);
                try {
                    String ntName = JsonUtils.getString(prodDto, "instance");
                    String operator = JsonUtils.getString(prodDto, "operator");
                    List<String> occurrences = JsonUtils.getStrings(prodDto, "occurrences");
                    SynthFunEvent.NonTerminal nonTerminal = grammar.get(ntName);
                    if (nonTerminal == null) { 
                        throw new DeserializationException(
                                String.format("Unknown nonterminal \"%s\" referenced in production", ntName),
                                "instance");
                    }
                    if (nonTerminal.productions().containsKey(operator)) { 
                        throw new DeserializationException(
                                String.format("Duplicate production \"%s\" for nonterminal \"%s\"", operator, ntName),
                                "operator");
                    }
                    for (int j = 0; j < occurrences.size(); j++) { 
                        if (!grammar.containsKey(occurrences.get(j))) {
                            throw new DeserializationException(
                                    String.format("Unknown nonterminal \"%s\" referenced in production child", ntName),
                                    "occurrences." + j);
                        }
                    }
                    nonTerminal.productions().put(operator, new SynthFunEvent.Production(operator, occurrences));
                } catch (DeserializationException e) {
                    throw e.prepend("productions." + i);
                }
            }
        } catch (DeserializationException e) {
            throw e.prepend("grammar");
        }
        return new SynthFunEvent(name, grammar, termType);
    }
private static SpecEvent parseSynthFun ( JSONObject eventDto ) throws DeserializationException { String name = JsonUtils . getString ( eventDto , "name" ) ; String termType = JsonUtils . getString ( eventDto , "termType" ) ; JSONObject grammarDto = JsonUtils . getObject ( eventDto , "grammar" ) ; Map < String , SynthFunEvent . NonTerminal > grammar = new HashMap < > ( ) ; try { List < JSONObject > nonTerminalsDto = JsonUtils . getObjects ( grammarDto , "nonTerminals" ) ; List < JSONObject > productionsDto = JsonUtils . getObjects ( grammarDto , "productions" ) ; for ( int i = 0 ; i < nonTerminalsDto . size ( ) ; i ++ ) { JSONObject ntDto = nonTerminalsDto . get ( i ) ; try { String ntName = JsonUtils . getString ( ntDto , "name" ) ; if ( grammar . containsKey ( ntName ) ) { throw new DeserializationException ( String . format ( "Duplicate nonterminal declaration \"%s\"" , ntName ) , "name" ) ; } grammar . put ( ntName , new SynthFunEvent . NonTerminal ( JsonUtils . getString ( ntDto , "termType" ) , new HashMap < > ( ) ) ) ; } catch ( DeserializationException e ) { throw e . prepend ( "nonTerminals." + i ) ; } } for ( int i = 0 ; i < productionsDto . size ( ) ; i ++ ) { JSONObject prodDto = productionsDto . get ( i ) ; try { String ntName = JsonUtils . getString ( prodDto , "instance" ) ; String operator = JsonUtils . getString ( prodDto , "operator" ) ; List < String > occurrences = JsonUtils . getStrings ( prodDto , "occurrences" ) ; SynthFunEvent . NonTerminal nonTerminal = grammar . get ( ntName ) ; if ( nonTerminal == null ) { throw new DeserializationException ( String . format ( "Unknown nonterminal \"%s\" referenced in production" , ntName ) , "instance" ) ; } if ( nonTerminal . productions ( ) . containsKey ( operator ) ) { throw new DeserializationException ( String . format ( "Duplicate production \"%s\" for nonterminal \"%s\"" , operator , ntName ) , "operator" ) ; } for ( int j = 0 ; j < occurrences . size ( ) ; j ++ ) { if ( ! grammar . containsKey ( occurrences . get ( j ) ) ) { throw new DeserializationException ( String . format ( "Unknown nonterminal \"%s\" referenced in production child" , ntName ) , "occurrences." + j ) ; } } nonTerminal . productions ( ) . put ( operator , new SynthFunEvent . Production ( operator , occurrences ) ) ; } catch ( DeserializationException e ) { throw e . prepend ( "productions." + i ) ; } } } catch ( DeserializationException e ) { throw e . prepend ( "grammar" ) ; } return new SynthFunEvent ( name , grammar , termType ) ; }
Deserializes a "synth-fun" event.
Deserializes a " synth - fun " event .
// parse target grammar specification // construct non-terminals // construct productions and attach them to their associated non-terminals // ensure non-terminal exists // ensure this production is distinct // ensure child term non-terminals exist