基于xtext的dsl实现元语言

textX的Python项目详细描述


https://raw.githubusercontent.com/textX/textX/master/art/textX-logo.png

PyPI Versionlicensebuild-statusDocumentation Status

textx是用python构建领域特定语言(dsl)的元语言。 它的灵感来自Xtext

简而言之,textx将帮助您以一种简单的方式构建文本语言。 你可以发明你自己的语言或者为已经存在的语言建立支持。 文本语言或文件格式。

从单一语言描述(语法)中,textx将构建 语法分析器和语言的元模型(也称为抽象语法)。 详细信息请参见文档。

textx遵循xtext的语法和语义,但是differs in some places是 使用Arpeggiopeg语法分析器在python中100%实现-无语法 模棱两可,前景无限,翻译风格。

快速介绍

下面是一个完整的示例,显示了 画画。我们还展示了如何定义自定义类、解释模型和搜索 对于特定类型的实例。

fromtextximportmetamodel_from_str,get_children_of_typegrammar="""
Model: commands*=DrawCommand;
DrawCommand: MoveCommand | ShapeCommand;
ShapeCommand: LineTo | Circle;
MoveCommand: MoveTo | MoveBy;
MoveTo: 'move' 'to' position=Point;
MoveBy: 'move' 'by' vector=Point;
Circle: 'circle' radius=INT;
LineTo: 'line' 'to' point=Point;
Point: x=INT ',' y=INT;
"""# We will provide our class for Point.# Classes for other rules will be dynamically generated.classPoint(object):def__init__(self,parent,x,y):self.parent=parentself.x=xself.y=ydef__str__(self):return"{},{}".format(self.x,self.y)def__add__(self,other):returnPoint(self.parent,self.x+other.x,self.y+other.y)# Create meta-model from the grammar. Provide `Point` class to be used for# the rule `Point` from the grammar.mm=metamodel_from_str(grammar,classes=[Point])model_str="""
    move to 5, 10
    line to 10, 10
    line to 20, 20
    move by 5, -7
    circle 10
    line to 10, 10
"""# Meta-model knows how to parse and instantiate models.model=mm.model_from_str(model_str)# At this point model is a plain Python object graph with instances of# dynamically created classes and attributes following the grammar.defcname(o):returno.__class__.__name__# Let's interpret the modelposition=Point(None,0,0)forcommandinmodel.commands:ifcname(command)=='MoveTo':print('Moving to position',command.position)position=command.positionelifcname(command)=='MoveBy':position=position+command.vectorprint('Moving by',command.vector,'to a new position',position)elifcname(command)=='Circle':print('Drawing circle at',position,'with radius',command.radius)else:print('Drawing line from',position,'to',command.point)position=command.pointprint('End position is',position)# Output:# Moving to position 5,10# Drawing line from 5,10 to 10,10# Drawing line from 10,10 to 20,20# Moving by 5,-7 to a new position 25,13# Drawing circle at 25,13 with radius 10# Drawing line from 25,13 to 10,10# Collect all points starting from the root of the modelpoints=get_children_of_type("Point",model)forpointinpoints:print('Point: {}'.format(point))# Output:# Point: 5,10# Point: 10,10# Point: 20,20# Point: 5,-7# Point: 10,10

视频教程

textx简介

https://img.youtube.com/vi/CN2IVtInapo/0.jpg

在textx

中实现martin fowler的状态机dsl https://img.youtube.com/vi/HI14jk0JIR0/0.jpg

文档和教程

完整的教程文档可在 http://textx.github.io/textX/stable/

IDE/编辑器中的支持

讨论和帮助

有关一般问题和帮助,请使用StackOverflow。一定要在你的问题上加上 textx标记。

有关问题、建议和功能要求,请使用 GitHub issue tracker

许可证

麻省理工学院

python版本

测试2.7、3.4+

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
Java算法:如何对实体进行分组   C语言中的Java X509EncodedKeySpec#   如何修复java lambda筛选器(缺少返回语句)与future   java有没有办法在Swing中以亚像素精度绘制线条?   HV000030:找不到约束“javax”的验证器。验证。限制。大小为“验证类型”的java。util。可选<java。lang.String>'   CQL中语句的java分页状态   在开发时使用applet查看器的java对象相关applet   java如何从Oracle获取时区ID而不是时区偏移量   java递增变量   java JavaFX InvalizationListener或ChangeListener   java使用多个数据包将大量XMP数据插入jpg?   java允许删除请求   java为什么与类同名的方法不需要返回类型?   java数据转换错误转换   java一旦布尔值为真,如何关闭JFrame?   如何将数据写入两个java。木卫一。一次输出流对象?   mysql如何使用java在SQL中划分两列?   java使用Saxon/XQuery设置URI或目录解析器   mysql java。sql。SQLException:无法将值“20200816 17:33:21.690”从第18列转换为时间戳