pegjs:如何处理前面有更通用类的字符类

问题描述

我的标识符可能包含点,但不能作为最后一个字符。例如,我想将“date.ymd”解析为标识符但“执行”。 as(标识符+标点符号)。 正则表达式将是 ([a-zA-z_][a-zA-Z0-9_.]*[a-zA-Z0-9_])|([a-zA-z_][a-zA-Z0-9_]?) 我怎样才能做到这一点?

我一直在尝试:

    program = identifier '.'
    identifier = piv:identifierValue {return {type: 'identifier',value: piv};}
    identifierValue
    = $(identifierHeadLetter (identifierTailLetter / '.')* identifierTailLetter*)
    identifierHeadLetter = letter / '_'
    identifierTailLetter "tail character" = letter / digit / '_'
    digit "digit" = '0' / '1' / '2' / '3' / '4' / '5' / '6' / '7' / '8' / '9'
    letter "letter" = 'a' / 'b' / 'c' / 'd' / 'e' / 'f' / 'g' / 'h' / 'i' / 'j' / 'k' / 'l' / 'm' / 'n' / 'o' / 'p' / 'q' / 'r' / 's' / 't' / 'u' / 'v' / 'w' / 'x' / 'y' / 'z' / 'A' / 'B' / 'C' / 'D' / 'E' / 'F' / 'G' / 'H' / 'I' / 'J' / 'K' / 'L' / 'M' / 'N' / 'O' / 'P' / 'Q' / 'R' / 'S' / 'T' / 'U' / 'V' / 'W' / 'X' / 'Y' / 'Z'

但以“execute.”作为输入,我得到: { “类型”:“标识符”, “值”:“执行。” }

如果在 identifierValue 的规则中我将“identifierTailLetter*”更改为“identifierTailLetter”,我得到: 第 1 行,第 9 列:预期的“。”或尾字符,但找到输入的结尾。

我怀疑我的问题与 PegJS - match all characters including ) except if ) is the last character 类似,但我找不到类似的解决方案。

解决方法

我自己回答。 就像将 identifierTailLetter 的规则更改为:

一样简单
identifierTailLetter "tail character"
 = letter / digit / '_' / ('.' &identifierTailLetter)

谢谢和抱歉