听起来解析器 的设计并不是为了使用正则表达式轻松处理词位大小。您应该检查从您的语法生成的骨架程序中的字符串大小。
为了说明,我从官网尝试了这个非常琐碎的语法示例:
"Name" = 'String Terminal Example'
"Author" = 'Devin Cook'
"About" = 'This is a simple example which defines a basic string'
"Start Symbol" = <Value>
! The following defines a set of characters for the string. It contains
! all printable characters with the exceptionof the double-quotes used
! for delimiters. The {Printable} set does not contain newlines.
{String Ch} = {Printable} - ["]
! This statement defines the string symbol
String = '"' {String Ch}* '"'
<Value> ::= String
String
既作为终端标记( String = '"' {String Ch}* '"'
) 也作为规则 ( <Value> ::= String
)。您可以在终端级别检查令牌大小。
我通过Calitha Engine - Custom Parser 类模板生成了一个C# ,我得到了一个解析器。下面我找到了您应该检查您的字符串终端令牌的部分:
// [...]
private Object CreateObjectFromTerminal(TerminalToken token)
{
switch (token.Symbol.Id)
{
// [...]
case (int)SymbolConstants.SYMBOL_STRING :
//String
//todo: Create a new object that corresponds to the symbol
return null;
// [...]
}
throw new SymbolException("Unknown symbol");
}
根据Calitha Parser Engine 文档,可以从令牌中检索文本:TerminalToken.Text
。那么为什么不进行如下操作:
case (int)SymbolConstants.SYMBOL_STRING :
// Check size (MAX_LENGTH could be a constant you defined)
if (token.Text.Length > MAX_LENGTH)
{
// handle error here
throw new SymbolException("String too long");
}
return token.Text;