当前位置: 首页 > wzjs >正文

模板网站的缺陷外包服务公司排名

模板网站的缺陷,外包服务公司排名,画册设计规范,网站 备案 查询前言 接着上篇:Java使用ANTLR4对Lua脚本语法校验,介绍了什么是ANTLR?/ 举了一个hello world示例 / ANTLR4 的工作流程。 解析IDL文件 准备两个IDL文件 V1.idl为架构的第一版约定文件 #ifndef xxx #define xxxmodule aaa {module bbb {mo…

前言

接着上篇:Java使用ANTLR4对Lua脚本语法校验,介绍了什么是ANTLR?/ 举了一个hello world示例 / ANTLR4 的工作流程。

解析IDL文件

准备两个IDL文件

V1.idl为架构的第一版约定文件

#ifndef xxx
#define xxxmodule aaa {module bbb {module ccc {struct ddd {unsigned short xxx1;unsigned long long xxx2;octet xxx3;};};  // module ccc};  // module bbb};  // module aaa#endif

V2.idl为架构的第二版约定文件

#ifndef xxx
#define xxxconst short aaa = 1;
module bbb {struct ccc {@default(0) uint8 xxx;};};module bbb {struct ddd {@default(255) uint8 xxx;};};
#endif

准备一个IDL Grammar文件

https://github.com/antlr/grammars-v4/tree/master/idl

maven配置

使用JDK8的注意:antlr4最高版本为4.9.3,原因如下:
来源:https://github.com/antlr/antlr4/releases/tag/4.10

Increasing minimum java version
Going forward, we are using Java 11 for the source code and the compiled .class files for the ANTLR tool. The Java runtime target, however, and the associated runtime tests use Java 8 (bumping up from Java 7).

<dependencies><dependency><groupId>org.antlr</groupId><artifactId>antlr4-runtime</artifactId><version>${antlr.version}</version></dependency>
</dependencies><build><plugins><plugin><groupId>org.antlr</groupId><artifactId>antlr4-maven-plugin</artifactId><version>${antlr.version}</version><configuration><visitor>true</visitor><listener>true</listener></configuration><executions><execution><goals><goal>antlr4</goal></goals></execution></executions></plugin></plugins>
</build><properties><!-- https://mvnrepository.com/artifact/org.antlr/antlr4-runtime --><!-- Antlr4 4.9.3 is the last version compatible with Java 8 --><antlr.version>4.9.3</antlr.version>
</properties>

生成Lexer Parser Listener Visitor代码

mvn clean compile

新建实体类

不知道咋出来这个界面的,看Java使用ANTLR4对Lua脚本语法校验 > 第一个例子
在这里插入图片描述
由上图,不难看出对象间对应关系:Specification对应多个Definition(Module/TypeDeclaration),TypeDeclaration对应多个Member。

package com.baeldung.antlr.idl;import java.util.ArrayList;
import java.util.List;/*** 规范** @author duhongming* @see* @since 1.0.0*/
public class Specification {private List<Definition> definitions;public Specification() {definitions = new ArrayList<>();}public void setDefinitions(List<Definition> children) {definitions = children;}public List<Definition> getDefinitions() {return definitions;}public void addChild(Definition child) {definitions.add(child);}
}
package com.baeldung.antlr.idl;/*** 定义** @author duhongming* @see* @since 1.0.0*/
public interface Definition {enum Kind {MODULE,INTERFACE,EXCEPTION,TYPE_DECLARATION,CONST_DECLARATION,ANNOTATION}boolean isIsModule();boolean isIsInterface();boolean isIsException();boolean isIsTypeDeclaration();boolean isIsConstDeclaration();boolean isIsAnnotation();
}
package com.baeldung.antlr.idl;/*** 模块** @author duhongming* @see* @since 1.0.0*/
public class Module implements Definition {private String name;private Definition child;public String getName() {return name;}public void setName(String name) {this.name = name;}public Definition getChild() {return child;}public void setChild(Definition child) {this.child = child;}@Overridepublic boolean isIsModule() {return true;}@Overridepublic boolean isIsInterface() {return false;}@Overridepublic boolean isIsException() {return false;}@Overridepublic boolean isIsTypeDeclaration() {return false;}@Overridepublic boolean isIsConstDeclaration() {return false;}@Overridepublic boolean isIsAnnotation() {return false;}
}
package com.baeldung.antlr.idl;import java.util.List;/*** 类型声明** @author duhongming* @see* @since 1.0.0*/
public class TypeDeclaration implements Definition {private String name;private List<Member> members;public String getName() {return name;}public void setName(String name) {this.name = name;}public List<Member> getMembers() {return members;}public void setMembers(List<Member> members) {this.members = members;}@Overridepublic boolean isIsModule() {return false;}@Overridepublic boolean isIsException() {return false;}@Overridepublic boolean isIsInterface() {return false;}@Overridepublic boolean isIsTypeDeclaration() {return true;}@Overridepublic boolean isIsConstDeclaration() {return false;}@Overridepublic boolean isIsAnnotation() {return false;}
}
package com.baeldung.antlr.idl;/*** 成员变量或方法** @author duhongming* @see* @since 1.0.0*/
public class Member {private String name;private String typeCode;public Member(String typeCode, String name) {super();this.typeCode = typeCode;this.name = name;}public String getName() {return name;}public void setName(String m_name) {this.name = m_name;}public String getTypeCode() {return typeCode;}public void setTypeCode(String typeCode) {this.typeCode = typeCode;}
}

IDL解析遍历器

package com.baeldung.antlr.idl;import com.baeldung.antlr.IDLBaseVisitor;
import com.baeldung.antlr.IDLParser;
import org.antlr.v4.runtime.tree.ParseTree;import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;public class IDLVisitor extends IDLBaseVisitor {@Overridepublic TypeDeclaration visitType_decl(IDLParser.Type_declContext ctx) {TypeDeclaration typeDeclaration = new TypeDeclaration();Optional<String> optional = Optional.ofNullable(ctx).map(IDLParser.Type_declContext::struct_type).map(IDLParser.Struct_typeContext::identifier).map(IDLParser.IdentifierContext::ID).map(ParseTree::getText);if (!optional.isPresent()) {return null;}typeDeclaration.setName(optional.get());List<IDLParser.MemberContext> members = ctx.struct_type().member_list().member();List<Member> memberList = new ArrayList<>();for (IDLParser.MemberContext member : members) {String type = member.type_spec().getText();String name = member.declarators().getText();Member m = new Member(type.replace("unsigned", ""), name);memberList.add(m);}typeDeclaration.setMembers(memberList);return typeDeclaration;}@Overridepublic Module visitModule(IDLParser.ModuleContext ctx) {Optional<String> optional = Optional.ofNullable(ctx).map(IDLParser.ModuleContext::identifier).map(IDLParser.IdentifierContext::ID).map(ParseTree::getText);if (!optional.isPresent()) {return null;}Module module = new Module();String moduleName = optional.get();module.setName(moduleName);List<IDLParser.DefinitionContext> definitions = ctx.definition();for (IDLParser.DefinitionContext definition : definitions) {Module subModule = visitModule(definition.module());if (Objects.nonNull(subModule)) {module.setChild(subModule);}TypeDeclaration typeDeclaration = visitType_decl(definition.type_decl());if (Objects.nonNull(typeDeclaration)) {module.setChild(typeDeclaration);}}return module;}@Overridepublic Specification visitSpecification(IDLParser.SpecificationContext ctx) {Specification specification = new Specification();if (ctx != null) {for (IDLParser.DefinitionContext definition : ctx.definition()) {Module module = visitModule(definition.module());if (Objects.nonNull(module)) {specification.addChild(module);}}}return specification;}
}

单元测试

package com.baeldung.antlr;import com.baeldung.antlr.idl.IDLVisitor;
import com.baeldung.antlr.idl.Specification;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.Lexer;
import org.antlr.v4.runtime.tree.ParseTree;
import org.junit.Test;import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;public class IDLParserUnitTest {public static Specification parseIdl(InputStream inputStream) throws IOException {return parseIdl(CharStreams.fromStream(inputStream));}public static Specification parseIdl(CharStream charStream) {// 用 in 构造词法分析器 lexer,词法分析的作用是将字符聚集成单词或者符号Lexer lexer = new IDLLexer(charStream);// 用词法分析器 lexer 构造一个记号流 tokensCommonTokenStream tokens = new CommonTokenStream(lexer);// 再使用 tokens 构造语法分析器 parser,至此已经完成词法分析和语法分析的准备工作IDLParser parser = new IDLParser(tokens);ParseTree tree = parser.specification();IDLVisitor visitor = new IDLVisitor();return (Specification) visitor.visit(tree);}@Testpublic void testV1() throws IOException {InputStream inputStream = Files.newInputStream(Paths.get("src/test/resources/V1.idl"));Specification specification = parseIdl(inputStream);System.out.println(specification);}@Testpublic void testV2() throws IOException {InputStream inputStream = Files.newInputStream(Paths.get("src/test/resources/V2.idl"));Specification specification = parseIdl(inputStream);System.out.println(specification);}
}

最终目录情况及单元测试情况如下:
在这里插入图片描述

参考

https://github.com/eProsima/IDL-Parser


文章转载自:

http://tWyTkz29.kqxng.cn
http://vSlyY89n.kqxng.cn
http://j12WcrED.kqxng.cn
http://hRPgWvhu.kqxng.cn
http://yl42oqlD.kqxng.cn
http://OYDrlkGx.kqxng.cn
http://3OkeMZq1.kqxng.cn
http://nNzpNSSN.kqxng.cn
http://h9lm9DRy.kqxng.cn
http://OMnvLTDk.kqxng.cn
http://IFRfaRxj.kqxng.cn
http://zQSfrSBN.kqxng.cn
http://CqQM9v17.kqxng.cn
http://cwQk1nnz.kqxng.cn
http://Ca7jntND.kqxng.cn
http://qpJQ6WqZ.kqxng.cn
http://gFI5EuYs.kqxng.cn
http://Lqt5CPxp.kqxng.cn
http://85HXs6y6.kqxng.cn
http://E4tfsbKR.kqxng.cn
http://VPhl28fP.kqxng.cn
http://qdIqiVOg.kqxng.cn
http://TaX5mRKF.kqxng.cn
http://6m0tDbr9.kqxng.cn
http://EiRHrk6Y.kqxng.cn
http://H2SHb8C2.kqxng.cn
http://8C0PRlfW.kqxng.cn
http://So596YIF.kqxng.cn
http://zGGrdc4f.kqxng.cn
http://GOSwMNNk.kqxng.cn
http://www.dtcms.com/wzjs/691629.html

相关文章:

  • 手机可以搭建网站吗电商平台排行榜前十名
  • 做公司网站需要花钱吗开发公司成本费用表格
  • 如何用wordpress快速建站在哪里做网站比较好
  • 广州好的网站建设信阳有什么推广平台
  • 北京公司建设网站网站制作要素
  • 廊坊网站建设方案策划网站设计与开发技术教程
  • 制作网站时搜索图标如何做郑州建设高端网站
  • 如何做网站用户活跃度网线制作评分标准
  • 做php网站需要什么软件开发wordpress怎样加快访问
  • 如何做网站联盟网站搭建详细步骤
  • 做网站怎么做鼠标跟随建设银行网站的特点优势
  • 管庄网站建设网页设计基础视频
  • 广州市白云区建设局网站WordPress多站点绑定域名
  • 做app网站的软件叫什么名字吗服务器和网站的关系
  • 90设计网站免费素材网站建设方案情况汇报
  • 商业网站的后缀一般为网络公司怎么做网站
  • 卸载西部数码网站管理助手网站建设需要多大的空间
  • 网站建设公司如何推广做一个网页难不难
  • 宝山网站推广信息服务公司的经营范围有哪些
  • 企业网站策划书模板范文wordpress代码演示
  • 手机网站页面范例基于阿里云的网站开发
  • 佛山专业网站营销安徽中机诚建建设有限公司网站
  • 网站做优化应该具备什么怎么网站建设公司
  • 湖北 网站备案网站代做
  • 西安建站套餐wordpress 信息发布
  • 做简单的网站链接郑州seo线上推广技术
  • 网站qq弹窗代码网站开发企业需要什么资质
  • php个人网站论文彭州网站建设
  • 网站建设 客户评价网站建设昆明
  • 点击网站首页域名又添加一个郑州品牌设计公司