You could write it like that:
    
    
    import std/[strutils, strformat]
    import /home/joel/daten/programming/nim/parlexgen/src/parlexgen
    
    type
      TokenKind = enum tkRect, tkSep, tkNum
      Token = object
        case kind: TokenKind
        of tkNum: val: float
        else: discard
      
      Rect = object
        x1, y1, x2, y2: float
    
    makeLexer lex[Token]:
      "Rect": Token(kind: tkRect)
      r";|,": Token(kind: tkSep)
      r"[0-9]+(.[0-9]+)": Token(kind: tkNum, val: parseFloat(match))
      
      r"[ \n\r]+": discard
    
    makeParser parse[Token]:
      rect[Rect]:
        if (tkRect, tkNum, tkSep, tkNum, tkSep, tkNum, tkSep, tkNum):
          Rect(x1: s2.val, y1: s4.val, x2: s6.val, y2: s8.val)
    
    func `$`(rect: Rect): string =
      fmt"Rect {rect.x1} {rect.y1} {rect.x2} {rect.y2}"
    
    echo $parse(lex("Rect 10.0    ;20.0,100  ,  200"))
    
    
    Run

but its not realy made for simple cases like that. Its made primarly for 
complex recursive things like starting from math expression up to programming 
languages

Reply via email to