@Test public void convertBasicMangaExample() throws IOException { InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("basicManga.to"); TurinParser.TurinFileContext root = new InternalParser().produceParseTree(inputStream); TurinFile expectedAst = basicMangaAst(); TurinFile actualAst = new ParseTreeToAst().toAst(root); assertEquals(expectedAst.getTopLevelPropertyDefinitions().size(), actualAst.getTopLevelPropertyDefinitions().size()); PropertyDefinition expProp = expectedAst.getTopLevelPropertyDefinitions().get(0); PropertyDefinition actProp = actualAst.getTopLevelPropertyDefinitions().get(0); assertEquals(expProp, actProp); assertEquals(expProp.getName(), actProp.getName()); assertEquals(expProp.getType(), actProp.getType()); assertEquals(expectedAst.getTopLevelPropertyDefinitions(), actualAst.getTopLevelPropertyDefinitions()); assertEquals(expectedAst, actualAst); }
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);  }