@staticmethod
def string_check(inputs):
matches = re.findall(
r"[\d一二两三四五六七八九十]([\-_-—\\\.\/::])[\d一二两三四五六七八九十]",
inputs
)
if not re.search("年", inputs) and len(matches) == 1:
return False
return True
def do_process(self, inputs: str) -> str:
assert self.string_check(inputs), "时间日期格式错误,请规范书写"
# 年份处理
year = self.__my_year.do_process(inputs)
if "~" in year:
return year
# 季度处理
quarter = self.__my_quarter.do_process(inputs, int(year))
if quarter is not None:
return quarter
# 月份处理
month = self.__my_month.do_process(inputs, int(year))
if month is None and not re.search(r"(周|日|号|天|礼拜|星期)", inputs):
return f"{year}-01-01~{year}-12-31"
# 星期处理
week = self.__my_week.do_process(inputs, month.split("~")[0] if month is not None else None)
if week is not None:
return week
# 日期处理
day = self.__my_day.do_process(inputs, month.split("~")[0] if month is not None else None)
if day is not None:
return day
if month is not None:
return month
return None
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.
def cname(o):
return o.class.name
Let's interpret the model
position = Point(None, 0, 0)
for command in model.commands:
if cname(command) == 'MoveTo':
print('Moving to position', command.position)
position = command.position
elif cname(command) == 'MoveBy':
position = position + command.vector
print('Moving by', command.vector, 'to a new position', position)
elif cname(command) == 'Circle':
print('Drawing circle at', position, 'with radius', command.radius)
else:
print('Drawing line from', position, 'to', command.point)
position = command.point
print('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 model
points = get_children_of_type("Point", model)
for point in points:
print('Point: {}'.format(point))
Output:
Point: 5,10
Point: 10,10
Point: 20,20
Point: 5,-7
Point: 10,10
Point
x
y
__add__
创建元模型(Meta-model)
使用 textX 的 metamodel_from_str 函数,根据提供的文法字符串创建了一个元模型,并将自定义的 Python 类 Point 传递给它,这样每当遇到文法中的 Point 规则时,都会创建我们自定义的 Point 对象。
解析模型字符串
文法已经定义,现在有一组字符串命令序列,这些字符串需要按照上面定义的文法进行解析。使用 model_from_str 方法可以根据提供的文法解析字符串,并创建一个模型对象,它是由 Point 类和其他动态生成的类实例组成的 Python 对象图。
from textx import metamodel_from_file
metamodel = metamodel_from_file('drawing.tx')
解析 DSL:
创建一个符合我们文法的例子 DSL 脚本 example.draw:
line from point 10,10 to point 20,20
circle center point 15,15 radius 5
然后解析它:
model = metamodel.model_from_file('example.draw')
使用解析后的模型:
一旦模型被解析,你可以像操作普通 Python 对象一样操作它们:
for command in model.commands:
if command.__class__.__name__ == "LineCommand":
print(f'Drawing line from {command.start.x},{command.start.y} to {command.end.x},{command.end.y}')
elif command.__class__.__name__ == "CircleCommand":
print(f'Drawing circle at {command.center.x},{command.center.y} with radius {command.radius}')