Skip to content

Commit

Permalink
remove multiline AST rendering for now, add Rudi renderer to turn AST…
Browse files Browse the repository at this point in the history
… back into Rudi code (closes #10)
  • Loading branch information
xrstf committed Dec 24, 2023
1 parent 287d550 commit 91fb71a
Show file tree
Hide file tree
Showing 14 changed files with 1,018 additions and 676 deletions.
4 changes: 1 addition & 3 deletions cmd/rudi/cmd/script/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"go.xrstf.de/rudi/cmd/rudi/options"
"go.xrstf.de/rudi/cmd/rudi/types"
"go.xrstf.de/rudi/cmd/rudi/util"
"go.xrstf.de/rudi/pkg/printer"

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

// show AST and quit if desired
if opts.PrintAst {
renderer := printer.AST{}
if err := renderer.WriteMultiline(program, os.Stdout); err != nil {
if err := program.DumpSyntaxTree(os.Stdout); err != nil {
return fmt.Errorf("failed to dump AST: %w", err)
}

Expand Down
17 changes: 16 additions & 1 deletion pkg/lang/ast/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,20 @@ package ast

import (
"fmt"
"regexp"
"strings"
)

// These variables must manually be kept in-sync with the Rudi grammar.
// Hoping that https://github.com/mna/pigeon/issues/141 will provide us with a better way.
// Compared to the original grammar, these regex are anchored to make matching easier.

var (
VariableNamePattern = regexp.MustCompile(`^[a-zA-Z_][a-zA-Z0-9_]*$`)
PathIdentifierPattern = VariableNamePattern
IdentifierNamePattern = regexp.MustCompile(`^[a-zA-Z_+/*_%?-][a-zA-Z0-9_+/*_%?!-]*$`)
)

type Expression interface {
String() string
ExpressionName() string
Expand Down Expand Up @@ -408,7 +419,11 @@ type EvaluatedPathStep struct {
func (a EvaluatedPathStep) String() string {
switch {
case a.StringValue != nil:
return fmt.Sprintf("[%q]", *a.StringValue)
if PathIdentifierPattern.MatchString(*a.StringValue) {
return fmt.Sprintf(".%s", *a.StringValue)
} else {
return fmt.Sprintf("[%q]", *a.StringValue)
}
case a.IntegerValue != nil:
return fmt.Sprintf("[%d]", *a.IntegerValue)
default:
Expand Down
5 changes: 4 additions & 1 deletion pkg/lang/grammar/rudi.peg
Original file line number Diff line number Diff line change
Expand Up @@ -249,10 +249,11 @@ ObjectAccessor <- '.' val:PathIdentifier {
return val, nil
}

VectorAccessor <- '[' expr:ScalarExpression ']' {
VectorAccessor <- '[' __ expr:ScalarExpression __ ']' {
return expr, nil
}

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

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

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

0 comments on commit 91fb71a

Please sign in to comment.