@Test public void typeExtendingAndImplementin() throws IOException { InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("parser_examples/type_extending_and_implementing.to"); TurinParser.TurinFileContext root = new InternalParser().produceParseTree(inputStream); TurinFile turinFile = new ParseTreeToAst().toAst(root); assertEquals(1, turinFile.getTopLevelTypeDefinitions().size()); TurinTypeDefinition typeDefinition = turinFile.getTopLevelTypeDefinitions().get(0); assertTrue(typeDefinition.getBaseType().isPresent()); assertEquals(2, typeDefinition.getInterfaces().size()); }
public TurinFile toAst(TurinParser.TurinFileContext ctx){ TurinFile turinFile = new TurinFile(); getPositionFrom(turinFile, ctx); turinFile.setNameSpace(toAst(ctx.namespace)); for (TurinParser.FileMemberContext memberCtx : ctx.fileMember()) { Node memberNode = toAst(memberCtx); if (memberNode instanceof TurinTypeDefinition) { turinFile.add((TurinTypeDefinition)memberNode); } else if (memberNode instanceof PropertyDefinition) { turinFile.add((PropertyDefinition) memberNode); } else if (memberNode instanceof Program) { turinFile.add((Program) memberNode); } else if (memberNode instanceof FunctionDefinitionNode) { turinFile.add((FunctionDefinitionNode)memberNode); } else if (memberNode instanceof RelationDefinition) { turinFile.add((RelationDefinition) memberNode); } else if (memberNode instanceof ContextDefinitionNode) { turinFile.add((ContextDefinitionNode) memberNode); } else { throw new UnsupportedOperationException(memberNode.getClass().getCanonicalName()); } } for (TurinParser.ImportDeclarationContext importDeclarationContext : ctx.importDeclaration()) { turinFile.add(toAst(importDeclarationContext)); } return turinFile; }
ParseTreeToAst { public TurinFile toAst(TurinParser.TurinFileContext ctx){ TurinFile turinFile = new TurinFile(); getPositionFrom(turinFile, ctx); turinFile.setNameSpace(toAst(ctx.namespace)); for (TurinParser.FileMemberContext memberCtx : ctx.fileMember()) { Node memberNode = toAst(memberCtx); if (memberNode instanceof TurinTypeDefinition) { turinFile.add((TurinTypeDefinition)memberNode); } else if (memberNode instanceof PropertyDefinition) { turinFile.add((PropertyDefinition) memberNode); } else if (memberNode instanceof Program) { turinFile.add((Program) memberNode); } else if (memberNode instanceof FunctionDefinitionNode) { turinFile.add((FunctionDefinitionNode)memberNode); } else if (memberNode instanceof RelationDefinition) { turinFile.add((RelationDefinition) memberNode); } else if (memberNode instanceof ContextDefinitionNode) { turinFile.add((ContextDefinitionNode) memberNode); } else { throw new UnsupportedOperationException(memberNode.getClass().getCanonicalName()); } } for (TurinParser.ImportDeclarationContext importDeclarationContext : ctx.importDeclaration()) { turinFile.add(toAst(importDeclarationContext)); } return turinFile; } }
ParseTreeToAst { public TurinFile toAst(TurinParser.TurinFileContext ctx){ TurinFile turinFile = new TurinFile(); getPositionFrom(turinFile, ctx); turinFile.setNameSpace(toAst(ctx.namespace)); for (TurinParser.FileMemberContext memberCtx : ctx.fileMember()) { Node memberNode = toAst(memberCtx); if (memberNode instanceof TurinTypeDefinition) { turinFile.add((TurinTypeDefinition)memberNode); } else if (memberNode instanceof PropertyDefinition) { turinFile.add((PropertyDefinition) memberNode); } else if (memberNode instanceof Program) { turinFile.add((Program) memberNode); } else if (memberNode instanceof FunctionDefinitionNode) { turinFile.add((FunctionDefinitionNode)memberNode); } else if (memberNode instanceof RelationDefinition) { turinFile.add((RelationDefinition) memberNode); } else if (memberNode instanceof ContextDefinitionNode) { turinFile.add((ContextDefinitionNode) memberNode); } else { throw new UnsupportedOperationException(memberNode.getClass().getCanonicalName()); } } for (TurinParser.ImportDeclarationContext importDeclarationContext : ctx.importDeclaration()) { turinFile.add(toAst(importDeclarationContext)); } return turinFile; }  }
ParseTreeToAst { public TurinFile toAst(TurinParser.TurinFileContext ctx){ TurinFile turinFile = new TurinFile(); getPositionFrom(turinFile, ctx); turinFile.setNameSpace(toAst(ctx.namespace)); for (TurinParser.FileMemberContext memberCtx : ctx.fileMember()) { Node memberNode = toAst(memberCtx); if (memberNode instanceof TurinTypeDefinition) { turinFile.add((TurinTypeDefinition)memberNode); } else if (memberNode instanceof PropertyDefinition) { turinFile.add((PropertyDefinition) memberNode); } else if (memberNode instanceof Program) { turinFile.add((Program) memberNode); } else if (memberNode instanceof FunctionDefinitionNode) { turinFile.add((FunctionDefinitionNode)memberNode); } else if (memberNode instanceof RelationDefinition) { turinFile.add((RelationDefinition) memberNode); } else if (memberNode instanceof ContextDefinitionNode) { turinFile.add((ContextDefinitionNode) memberNode); } else { throw new UnsupportedOperationException(memberNode.getClass().getCanonicalName()); } } for (TurinParser.ImportDeclarationContext importDeclarationContext : ctx.importDeclaration()) { turinFile.add(toAst(importDeclarationContext)); } return turinFile; }  TurinFile toAst(TurinParser.TurinFileContext ctx); }
ParseTreeToAst { public TurinFile toAst(TurinParser.TurinFileContext ctx){ TurinFile turinFile = new TurinFile(); getPositionFrom(turinFile, ctx); turinFile.setNameSpace(toAst(ctx.namespace)); for (TurinParser.FileMemberContext memberCtx : ctx.fileMember()) { Node memberNode = toAst(memberCtx); if (memberNode instanceof TurinTypeDefinition) { turinFile.add((TurinTypeDefinition)memberNode); } else if (memberNode instanceof PropertyDefinition) { turinFile.add((PropertyDefinition) memberNode); } else if (memberNode instanceof Program) { turinFile.add((Program) memberNode); } else if (memberNode instanceof FunctionDefinitionNode) { turinFile.add((FunctionDefinitionNode)memberNode); } else if (memberNode instanceof RelationDefinition) { turinFile.add((RelationDefinition) memberNode); } else if (memberNode instanceof ContextDefinitionNode) { turinFile.add((ContextDefinitionNode) memberNode); } else { throw new UnsupportedOperationException(memberNode.getClass().getCanonicalName()); } } for (TurinParser.ImportDeclarationContext importDeclarationContext : ctx.importDeclaration()) { turinFile.add(toAst(importDeclarationContext)); } return turinFile; }  TurinFile toAst(TurinParser.TurinFileContext ctx);  }