cgo: implement the constant parser as a real parser

Previously it was just a combination of heuristics to try to fit a
constant in an *ast.BasicLit. For more complex expressions, this is not
enough.

This change also introduces proper syntax error with locations, if
parsing a constant failed. For example, this will print a real error
message with source location:

    #define FOO 5)
This commit is contained in:
Ayke van Laethem
2019-11-04 16:30:57 +01:00
committed by Ron Evans
parent 5987233b99
commit cadb75a4aa
4 changed files with 207 additions and 55 deletions
+5 -2
View File
@@ -245,9 +245,12 @@ func tinygo_clang_globals_visitor(c, parent C.GoCXCursor, client_data C.CXClient
p.addError(pos, fmt.Sprintf("internal error: expected macro value to start with %#v, got %#v", name, source))
break
}
value := strings.TrimSpace(source[len(name):])
value := source[len(name):]
// Try to convert this #define into a Go constant expression.
expr := parseConst(pos, value)
expr, err := parseConst(pos+token.Pos(len(name)), p.fset, value)
if err != nil {
p.errors = append(p.errors, err)
}
if expr != nil {
// Parsing was successful.
p.constants[name] = constantInfo{expr, pos}