blynn / nex

Lexer for Go
http://cs.stanford.edu/~blynn/nex/
GNU General Public License v3.0
416 stars 47 forks source link

Add generic parse result interface to enhance Go Yacc integration. #19

Closed moorooboorai-jeroen closed 9 years ago

moorooboorai-jeroen commented 9 years ago

Go Yacc parse results currently best be passed back through the lexer, as mentioned by Russ Cox. Although this isn't a nice practice – in terms of decoupling and accompanying separation of concerns – it offers a midway until Go Yacc allows for 'proper' parser outcome communication.

Nex appears to recognize this situation by offering a 'hook' in the generated source code. However, this also introduces a separate search and replace step, most likely done in a Makefile build step or otherwise. Although workable, I think Go offers one step better by means of the interface{} 'type'. Which is what I'm proposing through this pull request.

As an example, code like below could be added to bottom of the .nex file:

//

package parser 

func MyParse(input string) (ast.Node, int) {
    inputReader := strings.NewReader(input) // ...might need import, depending on Nex -s switch usage

    lexer := NewLexer(inputReader)
    parseOutcome := yyParse(lexer)

    return lexer.parseResult, parseOutcome
}

Mind you, the ast.Node type is being returned, which could be replaced with anything to your liking. One could also wholly place the 'burden' of type assertion back to the caller by changing that to be interface{}. Which adds to my point.

Well, lot of words for a small change – :smiley: Hope I've got my point of view across.

blynn commented 9 years ago

Sounds good. Thanks!

moorooboorai-jeroen commented 9 years ago

Glad to be of help! Keep up the good work.