Example usage for org.antlr.v4.runtime CharStream size

List of usage examples for org.antlr.v4.runtime CharStream size

Introduction

In this page you can find the example usage for org.antlr.v4.runtime CharStream size.

Prototype

int size();

Source Link

Document

Returns the total number of symbols in the stream, including a single EOF symbol.

Usage

From source file:greycat.language.Model.java

License:Open Source License

private void internal_parse(CharStream in, Resolver resolver) {
    if (md == null) {
        try {/*ww  w  .j  a  v  a  2s.com*/
            md = MessageDigest.getInstance("SHA-1");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
    }
    if (in.size() == 0) {
        return;
    }

    String sha1 = convertToHex(md.digest(in.getText(new Interval(0, in.size())).getBytes()));
    if (alreadyLoaded.contains(sha1)) {
        return;
    } else {
        //circular dependency protection
        alreadyLoaded.add(sha1);
    }
    BufferedTokenStream tokens = new CommonTokenStream(new GreyCatModelLexer(in));
    GreyCatModelParser parser = new GreyCatModelParser(tokens);
    GreyCatModelParser.ModelDclContext modelDclCtx = parser.modelDcl();
    //first subImport
    modelDclCtx.importDcl().forEach(importDclContext -> {
        String subPath = cleanString(importDclContext.path);
        CharStream subStream = resolver.resolver(subPath);
        if (subStream == null) {
            throw new RuntimeException("Import not resolved " + subPath);
        }
        try {
            internal_parse(subStream, resolver);
        } catch (Exception e) {
            throw new RuntimeException("Parse Error while parsing " + subPath, e);
        }
    });
    // constants
    for (GreyCatModelParser.ConstDclContext constDclCtx : modelDclCtx.constDcl()) {
        String const_name = constDclCtx.name.getText();
        Constant c = constants.get(const_name);
        if (c == null) {
            c = new Constant(const_name);
            constants.put(const_name, c);
        }
        c.setType(constDclCtx.type.getText());
        String value = null;
        if (constDclCtx.constValueDcl() != null) {
            if (constDclCtx.constValueDcl().simpleValueDcl() != null) {
                GreyCatModelParser.SimpleValueDclContext sVc = constDclCtx.constValueDcl().simpleValueDcl();
                if (sVc.STRING() != null) {
                    value = cleanString(sVc.STRING().getSymbol());
                } else {
                    value = constDclCtx.constValueDcl().simpleValueDcl().getText();
                }
            } else if (constDclCtx.constValueDcl().taskValueDcl() != null) {
                GreyCatModelParser.TaskValueDclContext taskDcl = constDclCtx.constValueDcl().taskValueDcl();
                value = taskDcl.getText();
            }
        }
        c.setValue(value);
    }
    // classes
    for (GreyCatModelParser.ClassDclContext classDclCtx : modelDclCtx.classDcl()) {
        String classFqn = classDclCtx.name.getText();
        Class newClass = getOrAddClass(classFqn);
        // parents
        if (classDclCtx.parentDcl() != null) {
            final Class parentClass = getOrAddClass(classDclCtx.parentDcl().IDENT().getText());
            newClass.setParent(parentClass);
        }
        // annotations
        for (GreyCatModelParser.AnnotationDclContext annotationDcl : classDclCtx.annotationDcl()) {
            addAnnotation(newClass, annotationDcl);
        }
        // attributes
        for (GreyCatModelParser.AttributeDclContext attDcl : classDclCtx.attributeDcl()) {
            addAttribute(newClass, attDcl);
        }
        // relations
        for (GreyCatModelParser.RelationDclContext relDclCtx : classDclCtx.relationDcl()) {
            addRelation(newClass, relDclCtx);
        }
        // references
        for (GreyCatModelParser.ReferenceDclContext refDclCtx : classDclCtx.referenceDcl()) {
            addReference(newClass, refDclCtx);
        }
        // local indexes
        for (GreyCatModelParser.LocalIndexDclContext localIndexDclCtx : classDclCtx.localIndexDcl()) {
            addLocalIndex(newClass, localIndexDclCtx);
        }
        // local constants
        for (GreyCatModelParser.ConstDclContext constDclCtx : classDclCtx.constDcl()) {
            addLocalConstant(newClass, constDclCtx);
        }
    }

    // opposite management
    for (GreyCatModelParser.ClassDclContext classDclCtx : modelDclCtx.classDcl()) {
        String classFqn = classDclCtx.name.getText();
        Class classType = classes.get(classFqn);

        // relations
        for (GreyCatModelParser.RelationDclContext relDclCtx : classDclCtx.relationDcl()) {
            linkOppositeRelations(classType, relDclCtx);
        }
        // references
        for (GreyCatModelParser.ReferenceDclContext refDclCtx : classDclCtx.referenceDcl()) {
            linkOppositeReferences(classType, refDclCtx);
        }
        // local indexes
        for (GreyCatModelParser.LocalIndexDclContext idxDclCtx : classDclCtx.localIndexDcl()) {
            linkOppositeLocalIndexes(classType, idxDclCtx);
        }
    }

    // global indexes
    for (GreyCatModelParser.GlobalIndexDclContext globalIdxDclContext : modelDclCtx.globalIndexDcl()) {
        final String name = globalIdxDclContext.name.getText();
        final String type = globalIdxDclContext.type.getText();
        final Index index = getOrAddGlobalIndex(name, type);
        final Class indexedClass = getOrAddClass(index.type());
        for (TerminalNode idxDclIdent : globalIdxDclContext.indexAttributesDcl().IDENT()) {
            index.addAttributeRef(new AttributeRef(indexedClass.getOrCreateAttribute(idxDclIdent.getText())));
        }
    }

    // custom types
    for (GreyCatModelParser.CustomTypeDclContext customTypeDclCtx : modelDclCtx.customTypeDcl()) {
        String customTypeName = customTypeDclCtx.name.getText();
        final CustomType newCustomType = getOrAddCustomType(customTypeName);
        // parents
        if (customTypeDclCtx.parentDcl() != null) {
            final CustomType parentCustomType = getOrAddCustomType(
                    customTypeDclCtx.parentDcl().IDENT().getText());
            newCustomType.setParent(parentCustomType);
        }
        // attributes
        for (GreyCatModelParser.AttributeDclContext attDcl : customTypeDclCtx.attributeDcl()) {
            addAttribute(newCustomType, attDcl);
        }
        //            // relations
        //            for (GreyCatModelParser.RelationDclContext relDcl : customTypeDclCtx.relationDcl()) {
        //                addRelation(newCustomType, relDcl);
        //            }
        //            // references
        //            for (GreyCatModelParser.ReferenceDclContext refDcl : customTypeDclCtx.referenceDcl()) {
        //                addReference(newCustomType, refDcl);
        //            }
        //            // local indexes
        //            for (GreyCatModelParser.LocalIndexDclContext localIndexDcl : customTypeDclCtx.localIndexDcl()) {
        //                addLocalIndex(newCustomType, localIndexDcl);
        //            }
        // local constants
        for (GreyCatModelParser.ConstDclContext constDclCtx : customTypeDclCtx.constDcl()) {
            addLocalConstant(newCustomType, constDclCtx);
        }
        //            // opposite management
        //            for (GreyCatModelParser.CustomTypeDclContext typeDclCtx : modelDclCtx.customTypeDcl()) {
        //                String typeFqn = typeDclCtx.name.getText();
        //                CustomType customType = customTypes.get(typeFqn);
        //
        //                // relations
        //                for (GreyCatModelParser.RelationDclContext relDclCtx : typeDclCtx.relationDcl()) {
        //                    linkOppositeRelations(customType, relDclCtx);
        //                }
        //                // references

        //                for (GreyCatModelParser.ReferenceDclContext refDclCtx : typeDclCtx.referenceDcl()) {
        //                    linkOppositeReferences(customType, refDclCtx);
        //                }
        //                // local indexes
        //                for (GreyCatModelParser.LocalIndexDclContext idxDclCtx : typeDclCtx.localIndexDcl()) {
        //                    linkOppositeLocalIndexes(customType, idxDclCtx);
        //                }
        //            }

    }
}