Skip to content

Commit

Permalink
feat(parser): embed errors into the ast from the parser (#635)
Browse files Browse the repository at this point in the history
This change embeds errors into the AST when it is produced by the
parser.

Ideally, we want to have the parser generate the best AST it can and the
current parser doesn't do that. But, making that parser will take too
long at the current moment and we still need to record when we see an
invalid token. This change modifies `expect()` to record that it saw the
wrong token and then continue doing what it was already doing. When a
node is materialized, these aggregated errors are attached to the node.

Expect this functionality to change in the future while the mechanisms
for accessing errors will stay the same. This method does not give very
informative error messages and also does not retain the ability to
recreate the source code from the AST, but it does tell you if there's
a valid or invalid input that was consumed by the parser.
  • Loading branch information
jsternberg authored Jan 7, 2019
1 parent 9b53ac3 commit 7bf6bae
Show file tree
Hide file tree
Showing 4 changed files with 291 additions and 151 deletions.
8 changes: 8 additions & 0 deletions ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ func (p Position) Less(o Position) bool {
return p.Line < o.Line
}

func (p Position) IsValid() bool {
return p.Line > 0 && p.Column > 0
}

// SourceLocation represents the location of a node in the AST
type SourceLocation struct {
Start Position `json:"start"` // Start is the location in the source the node starts
Expand All @@ -43,6 +47,10 @@ func (l SourceLocation) Less(o SourceLocation) bool {
return l.Start.Less(o.Start)
}

func (l SourceLocation) IsValid() bool {
return l.Start.IsValid() && l.End.IsValid()
}

func (l *SourceLocation) Copy() *SourceLocation {
if l == nil {
return nil
Expand Down
Loading

0 comments on commit 7bf6bae

Please sign in to comment.