Skip to content
This repository was archived by the owner on Mar 16, 2025. It is now read-only.

Commit 91fb71a

Browse files
committed
remove multiline AST rendering for now, add Rudi renderer to turn AST back into Rudi code (closes #10)
1 parent 287d550 commit 91fb71a

14 files changed

Lines changed: 1018 additions & 676 deletions

File tree

cmd/rudi/cmd/script/command.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
"go.xrstf.de/rudi/cmd/rudi/options"
1616
"go.xrstf.de/rudi/cmd/rudi/types"
1717
"go.xrstf.de/rudi/cmd/rudi/util"
18-
"go.xrstf.de/rudi/pkg/printer"
1918

2019
"github.com/BurntSushi/toml"
2120
"gopkg.in/yaml.v3"
@@ -55,8 +54,7 @@ func Run(handler *util.SignalHandler, opts *options.Options, args []string) erro
5554

5655
// show AST and quit if desired
5756
if opts.PrintAst {
58-
renderer := printer.AST{}
59-
if err := renderer.WriteMultiline(program, os.Stdout); err != nil {
57+
if err := program.DumpSyntaxTree(os.Stdout); err != nil {
6058
return fmt.Errorf("failed to dump AST: %w", err)
6159
}
6260

pkg/lang/ast/types.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,20 @@ package ast
55

66
import (
77
"fmt"
8+
"regexp"
89
"strings"
910
)
1011

12+
// These variables must manually be kept in-sync with the Rudi grammar.
13+
// Hoping that https://github.com/mna/pigeon/issues/141 will provide us with a better way.
14+
// Compared to the original grammar, these regex are anchored to make matching easier.
15+
16+
var (
17+
VariableNamePattern = regexp.MustCompile(`^[a-zA-Z_][a-zA-Z0-9_]*$`)
18+
PathIdentifierPattern = VariableNamePattern
19+
IdentifierNamePattern = regexp.MustCompile(`^[a-zA-Z_+/*_%?-][a-zA-Z0-9_+/*_%?!-]*$`)
20+
)
21+
1122
type Expression interface {
1223
String() string
1324
ExpressionName() string
@@ -408,7 +419,11 @@ type EvaluatedPathStep struct {
408419
func (a EvaluatedPathStep) String() string {
409420
switch {
410421
case a.StringValue != nil:
411-
return fmt.Sprintf("[%q]", *a.StringValue)
422+
if PathIdentifierPattern.MatchString(*a.StringValue) {
423+
return fmt.Sprintf(".%s", *a.StringValue)
424+
} else {
425+
return fmt.Sprintf("[%q]", *a.StringValue)
426+
}
412427
case a.IntegerValue != nil:
413428
return fmt.Sprintf("[%d]", *a.IntegerValue)
414429
default:

pkg/lang/grammar/rudi.peg

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,10 +249,11 @@ ObjectAccessor <- '.' val:PathIdentifier {
249249
return val, nil
250250
}
251251

252-
VectorAccessor <- '[' expr:ScalarExpression ']' {
252+
VectorAccessor <- '[' __ expr:ScalarExpression __ ']' {
253253
return expr, nil
254254
}
255255

256+
// This Pattern must be kept in-sync with the PathIdentifierPattern variable in the ast package.
256257
PathIdentifier <- [a-zA-Z_][a-zA-Z0-9_]* {
257258
return ast.Identifier{Name: string(c.text)}, nil
258259
}
@@ -264,10 +265,12 @@ Variable <- '$' name:VariableName {
264265
return ast.Variable(name.(string)), nil
265266
}
266267

268+
// This Pattern must be kept in-sync with the VariableNamePattern variable in the ast package.
267269
VariableName <- [a-zA-Z_][a-zA-Z0-9_]* {
268270
return string(c.text), nil
269271
}
270272

273+
// This Pattern must be kept in-sync with the IdentifierNamePattern variable in the ast package.
271274
Identifier <- [a-zA-Z_+/*_%?-][a-zA-Z0-9_+/*_%?!-]* {
272275
name := string(c.text)
273276
bang := false

0 commit comments

Comments
 (0)