在 Gold Parser Builder 中定义 String/input 的长度

Defining length of a String/input in Gold Parser Builder

我是使用 Gold Parser Engine 的新手,正在寻找一种方法来限制已定义 string 的长度,但我没有找到任何方法来做到这一点。请帮助德做到这一点/。这是我的代码

    ! Welcome to GOLD Parser Builder 
"Case Sensitive"='false'
"Start Symbol" =<start>
{String Char}={Printable}
    <start>::=<Value>
        !<Value>::=<>|a<Value>|b<Value>
<Value>::=<type>name<equal>number<symbol>|<type>name<symbol>
        <type>::=int|float|char|double|boolean|String
            name={Letter}{alphanumeric}+
                <symbol>::=';'
                         <equal>::='='
number={Digit}+[.]{Digit}+|{Digit}+|{Letter} 

有什么方法可以解释字符串的最大限制。谢谢

听起来 解析器 的设计 无法轻松地处理词位大小表达式。你应该检查框架中的字符串大小程序生成从你的语法。

为了说明,我从官方网站this very trivial grammar example试了一下:

"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 class 模板生成了一个 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 documentation,可以从令牌中检索文本: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;