mirror of
https://github.com/google/go-jsonnet.git
synced 2026-05-05 12:06:11 +02:00
Move parser to its own package
This commit is contained in:
parent
0cd1ad28cd
commit
0d716ae56f
11
desugarer.go
11
desugarer.go
@ -24,6 +24,7 @@ import (
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/google/go-jsonnet/ast"
|
||||
"github.com/google/go-jsonnet/parser"
|
||||
)
|
||||
|
||||
func makeStr(s string) *ast.LiteralString {
|
||||
@ -39,7 +40,7 @@ func stringUnescape(loc *ast.LocationRange, s string) (string, error) {
|
||||
switch r {
|
||||
case '\\':
|
||||
if i >= len(s) {
|
||||
return "", MakeStaticError("Truncated escape sequence in string literal.", *loc)
|
||||
return "", parser.MakeStaticError("Truncated escape sequence in string literal.", *loc)
|
||||
}
|
||||
r2, w := utf8.DecodeRuneInString(s[i:])
|
||||
i += w
|
||||
@ -64,17 +65,17 @@ func stringUnescape(loc *ast.LocationRange, s string) (string, error) {
|
||||
buf.WriteRune('\t')
|
||||
case 'u':
|
||||
if i+4 > len(s) {
|
||||
return "", MakeStaticError("Truncated unicode escape sequence in string literal.", *loc)
|
||||
return "", parser.MakeStaticError("Truncated unicode escape sequence in string literal.", *loc)
|
||||
}
|
||||
codeBytes, err := hex.DecodeString(s[i : i+4])
|
||||
if err != nil {
|
||||
return "", MakeStaticError(fmt.Sprintf("Unicode escape sequence was malformed: %s", s[0:4]), *loc)
|
||||
return "", parser.MakeStaticError(fmt.Sprintf("Unicode escape sequence was malformed: %s", s[0:4]), *loc)
|
||||
}
|
||||
code := int(codeBytes[0])*256 + int(codeBytes[1])
|
||||
buf.WriteRune(rune(code))
|
||||
i += 4
|
||||
default:
|
||||
return "", MakeStaticError(fmt.Sprintf("Unknown escape sequence in string literal: \\%c", r2), *loc)
|
||||
return "", parser.MakeStaticError(fmt.Sprintf("Unknown escape sequence in string literal: \\%c", r2), *loc)
|
||||
}
|
||||
|
||||
default:
|
||||
@ -348,7 +349,7 @@ func desugar(astPtr *ast.Node, objLevel int) (err error) {
|
||||
|
||||
case *ast.Dollar:
|
||||
if objLevel == 0 {
|
||||
return MakeStaticError("No top-level object found.", *node.Loc())
|
||||
return parser.MakeStaticError("No top-level object found.", *node.Loc())
|
||||
}
|
||||
*astPtr = &ast.Var{NodeBase: node.NodeBase, Id: ast.Identifier("$")}
|
||||
|
||||
|
||||
@ -20,6 +20,7 @@ import (
|
||||
"fmt"
|
||||
|
||||
"github.com/google/go-jsonnet/ast"
|
||||
"github.com/google/go-jsonnet/parser"
|
||||
)
|
||||
|
||||
type ErrorFormatter struct {
|
||||
@ -36,7 +37,7 @@ func (ef *ErrorFormatter) format(err error) string {
|
||||
switch err := err.(type) {
|
||||
case RuntimeError:
|
||||
return ef.formatRuntime(&err)
|
||||
case StaticError:
|
||||
case parser.StaticError:
|
||||
return ef.formatStatic(&err)
|
||||
default:
|
||||
return ef.formatInternal(err)
|
||||
@ -48,7 +49,7 @@ func (ef *ErrorFormatter) formatRuntime(err *RuntimeError) string {
|
||||
// TODO(sbarzowski) pretty stuff
|
||||
}
|
||||
|
||||
func (ef *ErrorFormatter) formatStatic(err *StaticError) string {
|
||||
func (ef *ErrorFormatter) formatStatic(err *parser.StaticError) string {
|
||||
return err.Error() + "\n"
|
||||
// TODO(sbarzowski) pretty stuff
|
||||
}
|
||||
|
||||
@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package jsonnet
|
||||
package parser
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
@ -13,7 +13,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
package jsonnet
|
||||
package parser
|
||||
|
||||
import (
|
||||
"testing"
|
||||
@ -2,7 +2,7 @@
|
||||
// TypeWriter: set
|
||||
// Directive: +gen on literalField
|
||||
|
||||
package jsonnet
|
||||
package parser
|
||||
|
||||
// Set is a modification of https://github.com/deckarep/golang-set
|
||||
// The MIT License (MIT)
|
||||
@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package jsonnet
|
||||
package parser
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
@ -13,7 +13,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
package jsonnet
|
||||
package parser
|
||||
|
||||
import (
|
||||
"testing"
|
||||
@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package jsonnet
|
||||
package parser
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
@ -20,6 +20,7 @@ import (
|
||||
"fmt"
|
||||
|
||||
"github.com/google/go-jsonnet/ast"
|
||||
"github.com/google/go-jsonnet/parser"
|
||||
)
|
||||
|
||||
type analysisState struct {
|
||||
@ -77,7 +78,7 @@ func analyzeVisit(a ast.Node, inObject bool, vars ast.IdentifierSet) error {
|
||||
//nothing to do here
|
||||
case *ast.SuperIndex:
|
||||
if !inObject {
|
||||
return MakeStaticError("Can't use super outside of an object.", *a.Loc())
|
||||
return parser.MakeStaticError("Can't use super outside of an object.", *a.Loc())
|
||||
}
|
||||
visitNext(a.Index, inObject, vars, s)
|
||||
case *ast.Index:
|
||||
@ -121,13 +122,13 @@ func analyzeVisit(a ast.Node, inObject bool, vars ast.IdentifierSet) error {
|
||||
panic("Comprehensions not supported yet")
|
||||
case *ast.Self:
|
||||
if !inObject {
|
||||
return MakeStaticError("Can't use self outside of an object.", *a.Loc())
|
||||
return parser.MakeStaticError("Can't use self outside of an object.", *a.Loc())
|
||||
}
|
||||
case *ast.Unary:
|
||||
visitNext(a.Expr, inObject, vars, s)
|
||||
case *ast.Var:
|
||||
if !vars.Contains(a.Id) {
|
||||
return MakeStaticError(fmt.Sprintf("Unknown variable: %v", a.Id), *a.Loc())
|
||||
return parser.MakeStaticError(fmt.Sprintf("Unknown variable: %v", a.Id), *a.Loc())
|
||||
}
|
||||
s.freeVars.Add(a.Id)
|
||||
default:
|
||||
|
||||
5
vm.go
5
vm.go
@ -22,6 +22,7 @@ import (
|
||||
"runtime/debug"
|
||||
|
||||
"github.com/google/go-jsonnet/ast"
|
||||
"github.com/google/go-jsonnet/parser"
|
||||
)
|
||||
|
||||
// Note: There are no garbage collection params because we're using the native
|
||||
@ -98,11 +99,11 @@ func (vm *VM) EvaluateSnippet(filename string, snippet string) (json string, for
|
||||
}
|
||||
|
||||
func snippetToAST(filename string, snippet string) (ast.Node, error) {
|
||||
tokens, err := Lex(filename, snippet)
|
||||
tokens, err := parser.Lex(filename, snippet)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
node, err := Parse(tokens)
|
||||
node, err := parser.Parse(tokens)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user