为了使用 JAPE 语法构建 if-else 语句,并不总是需要在 RHS 中使用 Java。
在编写规则时,将处理划分为多个阶段通常很方便:每个阶段产生一些结果,然后可以将其传递给下一个阶段。所以,根据你刚才的描述,数据处理可以分为以下三个阶段。
- RecordFinder,它返回文档中的记录,即
Record
注释。
- TagFinder,它返回标签
a
和b
文档内。
- Intersection
a
:它在记录中搜索标签b
。
文件Main.jape
MultiPhase: Main
Phases:
RecordFinder
TagFinder
Intersection
文件RecordFinder.jape
此阶段能够注释文档中的记录。这个 JAPE 文件的唯一规则是读取标记(即标记Token
器返回的注释)作为输入,并在文档中查找记录(即标签record
),最后返回Record
注释。
请注意,在Options中,控件设置为first,因为目的是找到包含 token 的序列的第一次出现<record>
,然后是一个或多个其他 token ,然后是 token </record>
。
Phase: RecordFinder
Input: Token
Options: control = first debug = true
// The following rule is able to find the sentence within a record
Rule: RuleToFindRecord
(
({Token.string == "<"} {Token.string == "record"} ({Token})* {Token.string == ">"})
({Token})*
({Token.string == "<"} {Token.string == "/"} {Token.string == "record"} {Token.string == ">"})
):match
-->
:match.Record = { rule = "RuleToFindRecord" }
文件TagFinder.jape
此阶段读取标记a
作为输入并在文本中查找标签b
,最后返回a
和b
注释。
Phase: TagFinder
Input: Token
Options: control = first debug = true
// The following rule is able to find the tag "a" within the document.
Rule: RuleToFindTag_a
(
(
({Token.string == "<"} {Token.string == "a"} {Token.string == ">"})
({Token})*
({Token.string == "<"} {Token.string == "/"} {Token.string == "a"} {Token.string == ">"})
)
|
({Token.string == "<"} {Token.string == "a"} {Token.string == "/"} {Token.string == ">"})
):match
-->
:match.a = { rule = "RuleToFindTag_a" }
// The following rule is able to find the tag "b" within the document.
Rule: RuleToFindTag_b
(
(
({Token.string == "<"} {Token.string == "b"} {Token.string == ">"})
({Token})*
({Token.string == "<"} {Token.string == "/"} {Token.string == "b"} {Token.string == ">"})
)
|
({Token.string == "<"} {Token.string == "b"} {Token.string == "/"} {Token.string == ">"})
):match
-->
:match.b = { rule = "RuleToFindTag_b" }
文件交叉口.jape
此阶段读取注释Record
,a
并将b
其作为输入并搜索标签a
或b
内部Record
。阅读此作为有关contains和within运算符的参考(我在以下规则中使用了这些运算符之一)。
Phase: Intersection
Input: Record a b
Options: control = first debug = true
// A record matches with this rule if it contains both tag a and tag b.
Rule: Rule_1
(
{Record contains a, Record contains b}
):match
-->
:match.Record_with_both_tags = { rule = "Rule_1" }
// A record matches with this rule if it contains tag a.
Rule: Rule_2
(
{Record contains a}
):match
-->
:match.Record_with_tag_a = { rule = "Rule_2" }