lilt icon indicating copy to clipboard operation
lilt copied to clipboard

Add full example (including nim code) to docs

Open auxym opened this issue 6 years ago • 3 comments

My impression after reading the docs is that the grammar is very well explained. However, between "Example" and "Usage", I'm left a little confused at how to actually integrate lilt in a Nim program.

I think it would be nice if the example page had an actual nim snippet like the following:

import lilt

const grammar =  """
digit: <1234567890>
number: wholes=*digit ?["." decimals=+digit]
"""

let parsers = makeParsers grammar

let code = "12.3"

# I'm confused where to go from now, to obtain a LiltValue from our code string...

BTW, I'm very new at playing with parser generators and parsing in general, I'm just exploring nim and parsing for curiosity, and lilt seemed interesting. Sorry if this is obvious to everyone but me...

auxym avatar Dec 01 '18 22:12 auxym

After a bit of digging in Lilt source, I arrived at the following:

import lilt
import tables
import strutils

let parsers = makeParsers """
digit: <1234567890>
number: wholes=*digit ?["." decimals=+digit]
"""

let numberParser = parsers["number"]
let rootNode: LiltValue = numberParser "12.3"

# Utility function to print a line, indented to a given level
proc echoId(s: string, indent: Natural) =
    echo repeat("  ", indent), s

# Print out a lilt Node and its children recursively
func traverseAst(root: LiltValue, indent=0) = 
    case root.kind:
        of ltNode:
            echoId(root.node.kind & ":", indent)
            for k, v in root.node.properties:
                echoId(k & ":", indent + 1)
                traverseAst(v, indent + 2)

        of ltText:
            echoId("\"" & root.text & "\"", indent)

        of ltList:
            discard # Not sure here...

traverseAst rootNode

Which outputs:

number:
  decimals:
    "3"
  wholes:
    "12"

I think a similarly fleshed out example would help a lot in the docs!

auxym avatar Dec 02 '18 00:12 auxym

Hi! This is a great idea, so I have added one here. Also, if you haven't already read it, I recommend the Lilt tutorial.

Also, I feel compelled to warn you: while I'm definitely proud of Lilt, it's underdeveloped riddled with bugs. This is because once I finished implementing it, I had no real use for it and so development stopped. Beware!

However, if you have an interest actually using Lilt for non-toy programs, I would be willing to revive the project. I like the design of Lilt and would be interested to see where it goes.

Quelklef avatar Dec 02 '18 02:12 Quelklef

Unfortunately, I really am only toying.

From my cursory look at the source, I'd have to agree that the design looks good! And it currently appears to be one of two players in parsing tools for Nim, the other (combparser) does not have much documentation and does not seem much more mature than lilt.

auxym avatar Dec 02 '18 18:12 auxym