Skip to content

Commit

Permalink
Adds line:col to parse errors; Adds TokenUnknownRune
Browse files Browse the repository at this point in the history
  • Loading branch information
TekWizely committed Jan 30, 2020
1 parent f7094c7 commit 53fdc75
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 17 deletions.
3 changes: 2 additions & 1 deletion internal/lexer/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ func LexMain(_ *LexContext, l *lexer.Lexer) LexFn {
// Unknown
//
default:
l.EmitErrorf("unexpected rune '%c'", l.Next())
l.Next()
l.EmitToken(TokenUnknownRune)
return nil
}

Expand Down
2 changes: 2 additions & 0 deletions internal/lexer/tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,6 @@ const (

TokenScriptLine
TokenScriptEnd

TokenUnknownRune
)
27 changes: 11 additions & 16 deletions internal/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func parseMain(ctx *parseContext, p *parser.Parser) parseFn {
ctx.ast.AddScopeNode(&ast.ScopeVarAssignment{Name: name, Value: valueList})
ctx.ast.AddScopeNode(&ast.ScopeExportList{Names: []string{name}})
} else {
panic("expecting assignment values")
panic(parseError(p, "expecting assignment values"))
}
// '?='
//
Expand All @@ -126,7 +126,7 @@ func parseMain(ctx *parseContext, p *parser.Parser) parseFn {
ctx.ast.AddScopeNode(&ast.ScopeVarQAssignment{Name: name, Value: valueList})
ctx.ast.AddScopeNode(&ast.ScopeExportList{Names: []string{name}})
} else {
panic("expecting assignment values")
panic(parseError(p, "expecting assignment values"))
}
// ','
//
Expand Down Expand Up @@ -173,7 +173,7 @@ func parseMain(ctx *parseContext, p *parser.Parser) parseFn {
ctx.ast.AddScopeNode(&ast.ScopeAttrAssignment{Name: name, Value: valueList})
return parseMain
}
panic("expecting assignment value")
panic(parseError(p, "expecting assignment value"))
}
// Variable Assignment
//
Expand All @@ -183,7 +183,7 @@ func parseMain(ctx *parseContext, p *parser.Parser) parseFn {
ctx.ast.AddScopeNode(&ast.ScopeVarAssignment{Name: name, Value: valueList})
return parseMain
}
panic("expecting assignment value")
panic(parseError(p, "expecting assignment value"))
}
// Variable QAssignment
//
Expand All @@ -193,19 +193,14 @@ func parseMain(ctx *parseContext, p *parser.Parser) parseFn {
ctx.ast.AddScopeNode(&ast.ScopeVarQAssignment{Name: name, Value: valueList})
return parseMain
}
panic("expecting assignment value")
panic(parseError(p, "expecting assignment value"))
}
// Command
//
if ok = tryMatchCmd(ctx, p, nil); ok {
return parseMain
}
if p.CanPeek(1) {
t := p.Peek(1)
panic(fmt.Sprintf("%d:%d: Expecting command header", t.Line(), t.Column()))
} else {
panic("Expecting command header")
}
panic(parseError(p, "Expecting runfile statement"))
}

// tryMatchCmd
Expand All @@ -229,7 +224,7 @@ func tryMatchCmd(ctx *parseContext, p *parser.Parser, config *ast.CmdConfig) boo
}
if len(shell) > 0 {
if len(config.Shell) > 0 && shell != config.Shell {
panic(fmt.Sprintf("Shell '%s' defined in cmd header, shell '%s' defined in attributes", shell, config.Shell))
panic(parseError(p, fmt.Sprintf("Shell '%s' defined in cmd header, shell '%s' defined in attributes", shell, config.Shell)))
}
config.Shell = shell
}
Expand Down Expand Up @@ -313,7 +308,7 @@ func tryMatchDocBlock(ctx *parseContext, p *parser.Parser) (*ast.CmdConfig, bool
cmdConfig.Vars = append(cmdConfig.Vars, &ast.ScopeVarAssignment{Name: name, Value: valueList})
cmdConfig.Exports = append(cmdConfig.Exports, ast.NewScopeExportList1(name))
} else {
panic("expecting assignment value")
panic(parseError(p, "expecting assignment value"))
}
// '?='
//
Expand All @@ -323,7 +318,7 @@ func tryMatchDocBlock(ctx *parseContext, p *parser.Parser) (*ast.CmdConfig, bool
cmdConfig.Vars = append(cmdConfig.Vars, &ast.ScopeVarQAssignment{Name: name, Value: valueList})
cmdConfig.Exports = append(cmdConfig.Exports, ast.NewScopeExportList1(name))
} else {
panic("expecting assignment value")
panic(parseError(p, "expecting assignment value"))
}
// ','
//
Expand Down Expand Up @@ -507,7 +502,7 @@ func expectSubCmd(ctx *parseContext, p *parser.Parser) *ast.ScopeValueShell {
return &ast.ScopeValueShell{Cmd: ast.NewScopeValueNodeList(values)}
}
}
panic("expecting tokenRParen (')')")
panic(parseError(p, "expecting tokenRParen (')')"))
}

// expectSQString
Expand Down Expand Up @@ -566,7 +561,7 @@ func expectDQString(ctx *parseContext, p *parser.Parser) *ast.ScopeValueNodeList
return ast.NewScopeValueNodeList(values)
}
}
panic("expecting TokenDoubleQuote ('\"')")
panic(parseError(p, "expecting TokenDoubleQuote ('\"')"))
}

// tryMatchCmdHeaderWithShell matches [ [ 'CMD' ] DASH_ID ( '(' ID ')' )? ( ':' | '{' ) ]
Expand Down

0 comments on commit 53fdc75

Please sign in to comment.