mirror of
https://github.com/go-sylixos/elvish.git
synced 2024-12-01 00:33:05 +08:00
pkg/parse: Small API tweak.
This commit is contained in:
parent
fab4e82ed1
commit
c96bfade21
|
@ -151,7 +151,7 @@ func smartEnter(app cli.App) {
|
|||
}
|
||||
|
||||
func isSyntaxComplete(code string) bool {
|
||||
_, err := parse.Parse(parse.Source{Code: code})
|
||||
_, err := parse.Parse(parse.Source{Code: code}, parse.Config{})
|
||||
if err != nil {
|
||||
for _, e := range err.(*parse.Error).Entries {
|
||||
if e.Context.From == len(code) {
|
||||
|
|
|
@ -84,7 +84,7 @@ func Complete(code CodeBuffer, cfg Config) (*Result, error) {
|
|||
}
|
||||
|
||||
// Ignore the error; the function always returns a valid *ChunkNode.
|
||||
tree, _ := parse.Parse(parse.Source{Name: "[interactive]", Code: code.Content})
|
||||
tree, _ := parse.Parse(parse.Source{Name: "[interactive]", Code: code.Content}, parse.Config{})
|
||||
leaf := parseutil.FindLeafNode(tree.Root, code.Dot)
|
||||
for _, completer := range completers {
|
||||
ctx, rawItems, err := completer(leaf, cfg)
|
||||
|
|
|
@ -31,7 +31,7 @@ func highlight(code string, cfg Config, lateCb func(ui.Text)) (ui.Text, []error)
|
|||
var errors []error
|
||||
var errorRegions []region
|
||||
|
||||
tree, errParse := parse.Parse(parse.Source{Name: "[tty]", Code: code})
|
||||
tree, errParse := parse.Parse(parse.Source{Name: "[tty]", Code: code}, parse.Config{})
|
||||
if errParse != nil {
|
||||
for _, err := range errParse.(*parse.Error).Entries {
|
||||
if err.Context.From != len(code) {
|
||||
|
|
|
@ -172,6 +172,6 @@ func TestGetRegions(t *testing.T) {
|
|||
|
||||
func getRegionsFromString(code string) []region {
|
||||
// Ignore error.
|
||||
tree, _ := parse.Parse(parse.SourceForTest(code))
|
||||
tree, _ := parse.Parse(parse.SourceForTest(code), parse.Config{})
|
||||
return getRegions(tree.Root)
|
||||
}
|
||||
|
|
|
@ -143,7 +143,7 @@ func TestMakeHasCommand(t *testing.T) {
|
|||
}
|
||||
|
||||
func mustParse(src string) parse.Tree {
|
||||
tree, err := parse.Parse(parse.SourceForTest(src))
|
||||
tree, err := parse.Parse(parse.SourceForTest(src), parse.Config{})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
|
|
@ -38,7 +38,7 @@ func benchmarkEval(b *testing.B, code string) {
|
|||
ev := NewEvaler()
|
||||
src := parse.Source{Name: "[benchmark]", Code: code}
|
||||
|
||||
tree, err := parse.Parse(src)
|
||||
tree, err := parse.Parse(src, parse.Config{})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
|
|
@ -422,7 +422,7 @@ func (ev *Evaler) Eval(src parse.Source, cfg EvalCfg) error {
|
|||
cfg.fillDefaults()
|
||||
errFile := cfg.Ports[2].File
|
||||
|
||||
tree, err := parse.ParseWithDeprecation(src, errFile)
|
||||
tree, err := parse.Parse(src, parse.Config{WarningWriter: errFile})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -518,7 +518,7 @@ func (ev *Evaler) prepareFrame(src parse.Source, cfg EvalCfg) (*Frame, func()) {
|
|||
// return values may be non-nil. If w is not nil, deprecation messages are
|
||||
// written to it.
|
||||
func (ev *Evaler) Check(src parse.Source, w io.Writer) (*parse.Error, *diag.Error) {
|
||||
tree, parseErr := parse.ParseWithDeprecation(src, w)
|
||||
tree, parseErr := parse.Parse(src, parse.Config{WarningWriter: w})
|
||||
return parse.GetError(parseErr), ev.CheckTree(tree, w)
|
||||
}
|
||||
|
||||
|
|
|
@ -41,7 +41,7 @@ type Frame struct {
|
|||
// returns the altered local namespace, function that can be called to actuate
|
||||
// the evaluation, and a nil error.
|
||||
func (fm *Frame) PrepareEval(src parse.Source, r diag.Ranger, ns *Ns) (*Ns, func() Exception, error) {
|
||||
tree, err := parse.ParseWithDeprecation(src, fm.ErrorFile())
|
||||
tree, err := parse.Parse(src, parse.Config{WarningWriter: fm.ErrorFile()})
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
|
|
@ -53,7 +53,7 @@ func TestPurelyEvalCompound(t *testing.T) {
|
|||
t.Run(test.code, func(t *testing.T) {
|
||||
n := &parse.Compound{}
|
||||
err := parse.ParseAs(
|
||||
parse.Source{Name: "[test]", Code: test.code}, n, nil)
|
||||
parse.Source{Name: "[test]", Code: test.code}, n, parse.Config{})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
|
|
@ -26,25 +26,24 @@ type Tree struct {
|
|||
Source Source
|
||||
}
|
||||
|
||||
// Parse parses the given source. The returned error always has type *Error
|
||||
// if it is not nil.
|
||||
func Parse(src Source) (Tree, error) {
|
||||
return ParseWithDeprecation(src, nil)
|
||||
// Config keeps configuration options when parsing.
|
||||
type Config struct {
|
||||
// Destination of warnings. If nil, warnings are suppressed.
|
||||
WarningWriter io.Writer
|
||||
}
|
||||
|
||||
// ParseWithDeprecation is like Parse, but also writes out deprecation warnings
|
||||
// to the given io.Writer.
|
||||
func ParseWithDeprecation(src Source, w io.Writer) (Tree, error) {
|
||||
// Parse parses the given source. The returned error always has type *Error
|
||||
// if it is not nil.
|
||||
func Parse(src Source, cfg Config) (Tree, error) {
|
||||
tree := Tree{&Chunk{}, src}
|
||||
err := ParseAs(src, tree.Root, w)
|
||||
err := ParseAs(src, tree.Root, cfg)
|
||||
return tree, err
|
||||
}
|
||||
|
||||
// ParseAs parses the given source as a node, depending on the dynamic type of
|
||||
// n, writing deprecation warnings to the given io.Writer if it is not nil. If
|
||||
// the error is not nil, it always has type *Error.
|
||||
func ParseAs(src Source, n Node, w io.Writer) error {
|
||||
ps := &parser{srcName: src.Name, src: src.Code, warn: w}
|
||||
// n. If the error is not nil, it always has type *Error.
|
||||
func ParseAs(src Source, n Node, cfg Config) error {
|
||||
ps := &parser{srcName: src.Name, src: src.Code, warn: cfg.WarningWriter}
|
||||
ps.parse(n)
|
||||
ps.done()
|
||||
return ps.assembleError()
|
||||
|
|
|
@ -297,7 +297,7 @@ var goodCases = []struct {
|
|||
func TestParse(t *testing.T) {
|
||||
for _, tc := range goodCases {
|
||||
src := SourceForTest(tc.src)
|
||||
tree, err := Parse(src)
|
||||
tree, err := Parse(src, Config{})
|
||||
if err != nil {
|
||||
t.Errorf("Parse(%q) returns error: %v", tc.src, err)
|
||||
}
|
||||
|
@ -367,7 +367,7 @@ var parseErrorTests = []struct {
|
|||
func TestParseError(t *testing.T) {
|
||||
for _, test := range parseErrorTests {
|
||||
t.Run(test.src, func(t *testing.T) {
|
||||
_, err := Parse(SourceForTest(test.src))
|
||||
_, err := Parse(SourceForTest(test.src), Config{})
|
||||
if err == nil {
|
||||
t.Fatalf("no error")
|
||||
}
|
||||
|
|
|
@ -25,7 +25,7 @@ descend:
|
|||
|
||||
// Wordify turns a piece of source code into words.
|
||||
func Wordify(src string) []string {
|
||||
tree, _ := parse.Parse(parse.Source{Name: "[wordify]", Code: src})
|
||||
tree, _ := parse.Parse(parse.Source{Code: src}, parse.Config{})
|
||||
return wordifyInner(tree.Root, nil)
|
||||
}
|
||||
|
||||
|
|
|
@ -64,7 +64,7 @@ func TestPPrintParseTree(t *testing.T) {
|
|||
}
|
||||
|
||||
func mustParse(src string) Node {
|
||||
tree, err := Parse(SourceForTest(src))
|
||||
tree, err := Parse(SourceForTest(src), Config{})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user