基于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类。getConstructor找不到兼容的构造函数   java单元测试自定义输入和输出格式   javascript Java UI线程内存不足错误被阻止   多线程在Java并发API中从线程池中选择线程的逻辑是什么?   java在MySQL数据库上存储歌曲(BLOb支持)   java快速且可扩展的IO传递   jsf c:forEach-inside-primeface(例如p:panelgrid)inside-ui:repeat   tomcat与JavaServlet3.0文件上传,如何获取maxfilesize值   java JNI,加载2次相同的dll(不同的名称),如何选择使用哪一个?   java将InputStream复制到OutputStream将停止,除非我也向系统写入。出来   java Set反向安全约束   比如在java中使用整数时?   java使用@接口抑制IntelliJ中的某些警告   java我的ASCII艺术在JLabel上是错误的   JavaSpring工具源代码(或带有SpringIde的Eclipse)崩溃   向片段添加ListView时出现java错误   循环Java循环错误需要重新审视   JavaAspectJ:匹配GenericServlet。带注释子类实例的init()调用