clojure - 允许任意规则顺序的语法
问题描述
我正在(尝试)设计一种特定领域的语言(我称之为“华氏”)来设计引用风格。
用华氏温度编写的程序:
- 必须正好有一个
citation
块 - 可能有零个或多个
macro
块。
这是一个简化但有效的示例:
macro m1
"Hello World!"
end
macro m2
"Hello World!"
end
citation
"Hello World!"
end
该语法将把上面的代码识别为语法正确:
style = macro* citation
(* example of macro definition
macro hw
"Hello World!"
end
*)
macro = <'macro'> #'[a-z0-9]+' statement+ end
citation = <'citation'> statement+ end
statement = #'".*?"'
<end> = <'end'>
然而,“块”的顺序(例如macro
or citation
)应该无关紧要。
问题:我应该如何更改我的语法,以便它识别以下程序在语法上是正确的?
macro m1
"Hello World!"
end
citation
"Hello World!"
end
macro m2
"Hello World!"
end
PS:我打算添加其他顺序也无关紧要的可选块。
解决方案
对于 0..n 规则,您可以将它们放在citation
. 例如
style = tools* citation tools*
tools = macro | foo | bar
...
推荐阅读
- python - 在同一个项目中使用 Fabric 2 和 Fabric 3
- ruby - Rspec 未通过测试,其中 'Diff:' 看起来相同
- vega-lite - 变通以显示基于周期时间列的 vega-lite 图表?
- google-data-studio - 使用 CASE WHEN 语句和聚合创建字段失败
- angular - 从 Angular 应用程序启动/停止 docker 的容器
- mongodb - 包含日期范围的最佳 Mongo 索引策略是什么
- django - 如何覆盖 Django 管理员/表单用于外键的查询集
- mysql - SQL 查询建议
- javascript - 如何编写使用异步函数和流的函数?
- spartacus-storefront - 接收页面模板的延迟加载模块