by weberc2 · 8y ago · 0 comments
I would love to be pointed in the right direction!
A 30 second overview:
The first thing you need to do is scan your file for tokens (eg "{", "}", "[", "]", ",", "\"", etc in the case of JSON).
Then you traverse your tokens and turn them into an AST (Abstract Syntax Tree) which is essentially just a "struct" version of the code (in the dumbest description imaginable). The point of this is you start adding definitions to your tokens. Eg what does a "[" mean?
From there you can start writing your real business logic since you now have a Go struct version of your code which you can traverse.
However I would really would recommend doing a google for how to write your own compiler. There are dozens of good resources out there (I can't recall the one that I used off hand) and they'll explain this process a thousand times better than I have above.
One last thing to note, the theory of writing a parser is pretty language agnostic so don't get put off that one guide is in C or another in Java or Python or LISP or whatever. As long as you get the theory nailed you can write the Go code equivalent without too much headache.
For example, let's say we start with a function that parses a character '['.
buf, pos := []byte("[123]"), 0
LBRACK := func()bool {
if buf[pos] != '[' {
return false
}
pos ++ // consume character
return true
}
It would suck to write such function for every single character we have to parse. We can generate it instead: CHAR := func(c byte)func()bool {
return func()bool {
if buf[pos] != c {
return false
}
pos ++
return true
}
}
LBRACK := CHAR('[')
Now let's do the same for parsing a sequence. We combine outputs of other functions and return one that parses them one by one. SEQ := func (args... func()bool) func()bool {
return func()bool {
for i := 0; i < len(args); i++ {
f := args[i]
if f() == false {
return false
}
}
return true
}
}
parser := SEQ(
CHAR('['),
SEQ(
CHAR('1'),
CHAR('2'),
CHAR('3'),
),
CHAR(']'),
)
This is the idea.This is not a real world example though. You'll need to have more primitives, a wrapper that handles various things, like restoring buffer index on failure, captures, that save starting index, move buf/pos into context structure, virtual stack to store captures/errors, etc.
func SEQ (args... func(ctx *Ctx)bool) func(ctx *Ctx)bool {
return wrap (func(ctx *Ctx)bool {
...
})
}
But it's important to get there on your own. And it's pretty much as fast and as flexible as completely hand-written recursive descent parsers and as powerful as parser generators. SEQ(
...
CAPTURE(
// parser to capture
SEQ(...),
// extra arguments:
// a name, a function to call,
// that creates an AST node, etc.
),
)