Skip to content

Commit 9f539ff

Browse files
authored
Merge pull request #160 from hinshun/issue-144
Break up bad lexemes by spaces so that we can return the correct syntax error
2 parents 532bacc + 8c565be commit 9f539ff

File tree

4 files changed

+24
-6
lines changed

4 files changed

+24
-6
lines changed

checker/checker.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func (c *checker) Check(mod *parser.Module) error {
3535
switch n := node.(type) {
3636
case *parser.Decl:
3737
if n.Bad != nil {
38-
c.errs = append(c.errs, ErrBadParse{n})
38+
c.errs = append(c.errs, ErrBadParse{n, n.Bad.Lexeme})
3939
return false
4040
}
4141
case *parser.ImportDecl:
@@ -286,7 +286,7 @@ func (c *checker) checkBlockStmt(scope *parser.Scope, typ parser.ObjType, block
286286

287287
for _, stmt := range block.NonEmptyStmts() {
288288
if stmt.Bad != nil {
289-
c.errs = append(c.errs, ErrBadParse{stmt})
289+
c.errs = append(c.errs, ErrBadParse{stmt, stmt.Bad.Lexeme})
290290
continue
291291
}
292292

@@ -476,7 +476,7 @@ func (c *checker) checkExpr(scope *parser.Scope, typ parser.ObjType, expr *parse
476476
var err error
477477
switch {
478478
case expr.Bad != nil:
479-
err = ErrBadParse{expr}
479+
err = ErrBadParse{expr, expr.Bad.Lexeme}
480480
case expr.Selector != nil:
481481
// Do nothing for now.
482482
case expr.Ident != nil:

checker/checker_test.go

+17
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,23 @@ func TestChecker_Check(t *testing.T) {
373373
"no error when input doesn't end with newline",
374374
`# comment\nfs default() {\n scratch\n}\n# comment`,
375375
nil,
376+
}, {
377+
"errors when go-style filemode is used as arg",
378+
`
379+
fs default() {
380+
mkfile "foo" 0644 "content"
381+
}
382+
`,
383+
ErrBadParse{
384+
Node: &parser.Bad{
385+
Pos: lexer.Position{
386+
Filename: "<stdin>",
387+
Line: 2,
388+
Column: 14,
389+
},
390+
},
391+
Lexeme: "0644",
392+
},
376393
}} {
377394
tc := tc
378395
t.Run(tc.name, func(t *testing.T) {

checker/errors.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,12 @@ func (e ErrImportNotExist) Error() string {
128128
}
129129

130130
type ErrBadParse struct {
131-
Node parser.Node
131+
Node parser.Node
132+
Lexeme string
132133
}
133134

134135
func (e ErrBadParse) Error() string {
135-
return fmt.Sprintf("%s unable to parse", FormatPos(e.Node.Position()))
136+
return fmt.Sprintf("%s unable to parse %q", FormatPos(e.Node.Position()), e.Lexeme)
136137
}
137138

138139
type ErrUseModuleWithoutSelector struct {

parser/cst.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ var (
3030
Newline = \n
3131
Operator = {|}|\(|\)|,|;
3232
Comment = #[^\n]*\n
33-
Bad = .*
33+
Bad = [^\r\t\n ]*
3434
`)))
3535

3636
// Parser parses HLB into a concrete syntax tree rooted from a Module.

0 commit comments

Comments
 (0)