Skip to content

Instantly share code, notes, and snippets.

@jeanbza
Created July 9, 2021 18:13
Show Gist options
  • Save jeanbza/d7062992d61849e19c4baa33b49f349f to your computer and use it in GitHub Desktop.
Save jeanbza/d7062992d61849e19c4baa33b49f349f to your computer and use it in GitHub Desktop.
language errors are quite confusing. ex, the following:
digraph: language error: DocLanguage:20:20: expect '\n' / $ got ".*\nTranspose <- tran"
(for the following language:
const language = `
Nodes <- nodes .*
Transpose <- transpose .*
Somepath <- somepath .*
`
)
this is quite confusing. in general, providing a proper language is confusing and difficult. it relies very heavily on a variety of regex, which is going to confuse a large number of users. maybe that's OK, though, if it's just used in tools? but, let me continue my point:
it's much more complicated than flags, for example, where I just define int/string/etc types, with a name, default value, and usage note. it would be nice if there were some shims that allowed providing flags-like definitions, as well as an escape hatch where you could add the regex style language
---------------------
I see now that my language errors are somehow due to needing to provide match groups. Do all languages need them?
I see also that I have to quote words in my language, or else it goes and tries to find them in other sections of the language (ex Key in the example_test). Hm. This is, like, quasi regex. That makes it more confusing :)
---------------------
the example_test is fantastic, and very necessary. but it could be improved:
- there should be a very simple example
- the current example shows a LOT of things. specifically, it took me a while to realise that it's showing how some commands can reference other commands. it doesn't seem necessary, but is a neat feature i could see being used if there's a lot of repeat (ex if Key is used in many places)
- there should be examples of doing things the PEG way for everything you can do with flags
---------------------
after fixing my language errors, I got:
digraph: no match
This appears to be a match error in expressions.go. What does it mean? What is a match? What didn't match? How should it match? What should I fix?
---------------------
it's a little bit annoying having to do 3 steps to go from os.Args to a parser:
in := strings.Join(os.Args, " ")
r := strings.NewReader(in)
_, err = p.Parse("input", r)
it'd be nice if there were a p.Parse that parsed os.Args. like, p.ParseArgs, or something, that takes no arguments
(is "input" necessary? I have no idea what that refers to. can it be hardcoded?)
---------------------
i realise a lot of my comments are about using PEG to replace flags, which is not the only thing you can do with PEG. however, that's the lens I'm looking at it through, hence these comments =)
---------------------
still struggling to fix the "no match" issue. every command i've defined is no matching. current language:
const language = `
Nodes <- "nodes"
Transpose <- "tranpose"
Somepath <- "somepath" '([\w]+)' '([\w]+)'
`
not a lot to go on wrt what's wrong with my language, or inputs
---------------------
I see the following in grammar.peg: Expression <- Choice
what's the point of this?
---------------------
figured out the problem: I was using os.Args, which translates to "./digraph nodes", when I should have been using os.Args[1:]. very easy to misuse
---------------------
wonder how one defines subcommands, and flags. will investigate after i get this working
---------------------
so, "nodes" works, but "transpose" and "somepath" doesnt. I kind of get somepath, because it has groups, and I'm likely doing that wrong. but transpose is exactly the same as nodes: it just has a single word, with no arguments. what the heck?
oooooooh. you have to define a state machine from the _start_. I don't really know how to express this is in proper words - it's a bit complicated. but, I get nodes and transpose with the following language:
const language = `
File <- Line :$
Line <- !$ (Nodes)?(Transpose)?(Somepath)? :$
Nodes <- "nodes"
Transpose <- "transpose"
Somepath <- "somepath" '([\w]+)' '([\w]+)'
`
somepath still needs work
wew, this language needs to be more complicated than expected. i think it's going to get really hairy with subcommands and flags
---------------------
once again, expect and got is quite confusing:
digraph: input:0:0: expect $ got "somepath foo bar"
---------------------
ah, spaces need to be encoded as tokens. the following language allows my somepath command to work:
const language = `
File <- Line :$
Line <- !$ (Nodes / Transpose / Somepath)? :$
Nodes <- "nodes"
Transpose <- "transpose"
Somepath <- "somepath" _ '([\w]+)' _ '([\w]+)'
_ <- '[ \t]*'?
`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment