Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 138 additions & 0 deletions parser/additional_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import (
"errors"
"fmt"
"io"
"strings"
"testing"
Expand Down Expand Up @@ -375,3 +376,140 @@
}
}
}

func TestParseExpr(t *testing.T) {
tests := []struct {
name string
input string
wantErr bool
}{
{
name: "simple integer",
input: "42",
wantErr: false,
},
{
name: "simple arithmetic",
input: "2 + 3",
wantErr: false,
},
{
name: "complex arithmetic",
input: "2 + 3 * 4",
wantErr: false,
},
{
name: "boolean expression",
input: "(x > 5) && (y < 10)",
wantErr: false,
},
{
name: "string literal",
input: `"hello world"`,
wantErr: false,
},
{
name: "list literal",
input: "{1, 2, 3, 4, 5}",
wantErr: false,
},
{
name: "function call",
input: `strcat("Hello", " ", "World")`,
wantErr: false,
},
{
name: "conditional expression",
input: `x > 0 ? "positive" : "non-positive"`,
wantErr: false,
},
{
name: "attribute reference",
input: "MY.attr",
wantErr: false,
},
{
name: "boolean literal",
input: "true",
wantErr: false,
},
{
name: "undefined literal",
input: "undefined",
wantErr: false,
},
{
name: "error literal",
input: "error",
wantErr: false,
},
{
name: "record literal",
input: "[a = 1; b = 2]",
wantErr: false,
},
{
name: "nested expression",
input: "((1 + 2) * (3 + 4))",
wantErr: false,
},
{
name: "syntax error",
input: "2 + +",
wantErr: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
expr, err := ParseExpr(tt.input)
if tt.wantErr {
if err == nil {
t.Fatalf("ParseExpr(%q) expected error, got nil", tt.input)
}
return
}
if err != nil {
t.Fatalf("ParseExpr(%q) unexpected error: %v", tt.input, err)
}
if expr == nil {
t.Fatalf("ParseExpr(%q) returned nil expression", tt.input)
}
// Verify it implements ast.Expr interface
if _, ok := expr.(ast.Expr); !ok {

Check failure on line 479 in parser/additional_parser_test.go

View workflow job for this annotation

GitHub Actions / Lint

S1040: type assertion to the same type: expr already has type ast.Expr (staticcheck)
t.Fatalf("ParseExpr(%q) result does not implement ast.Expr", tt.input)
}
})
}
}

func TestParseExprTypeChecking(t *testing.T) {
// Test that ParseExpr returns different types for different inputs
tests := []struct {
name string
input string
expectedTyp string
}{
{"integer", "42", "*ast.IntegerLiteral"},
{"real", "3.14", "*ast.RealLiteral"},
{"string", `"hello"`, "*ast.StringLiteral"},
{"boolean", "true", "*ast.BooleanLiteral"},
{"list", "{1, 2}", "*ast.ListLiteral"},
{"binary op", "1 + 2", "*ast.BinaryOp"},
{"attribute ref", "x", "*ast.AttributeReference"},
{"function call", "func()", "*ast.FunctionCall"},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
expr, err := ParseExpr(tt.input)
if err != nil {
t.Fatalf("ParseExpr(%q) unexpected error: %v", tt.input, err)
}
typeName := fmt.Sprintf("%T", expr)
if typeName != tt.expectedTyp {
t.Fatalf("ParseExpr(%q) returned type %s, want %s", tt.input, typeName, tt.expectedTyp)
}
})
}
}
6 changes: 6 additions & 0 deletions parser/classad.y
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ start
lex.SetResult($1)
}
}
| expr
{
if lex, ok := yylex.(interface{ SetResult(ast.Node) }); ok {
lex.SetResult($1)
}
}
;

classad
Expand Down
19 changes: 17 additions & 2 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@ import (
"github.com/PelicanPlatform/classad/ast"
)

// Parse parses a ClassAd expression string and returns the AST.
// Parse parses a ClassAd or expression string and returns the AST.
// For ClassAd-only parsing, use ParseClassAd. For expression-only parsing, use ParseExpr.
func Parse(input string) (ast.Node, error) {
lex := NewLexer(input)
yyParse(lex)
return lex.Result()
}

// ParseClassAd parses a ClassAd and returns a ClassAd AST node.
// Returns an error if the input is not a valid ClassAd (e.g., if it's a bare expression).
func ParseClassAd(input string) (*ast.ClassAd, error) {
node, err := Parse(input)
if err != nil {
Expand All @@ -26,7 +28,20 @@ func ParseClassAd(input string) (*ast.ClassAd, error) {
if classad, ok := node.(*ast.ClassAd); ok {
return classad, nil
}
return nil, nil
return nil, fmt.Errorf("parsed input is not a ClassAd, got %T", node)
}

// ParseExpr parses an expression string and returns the expression AST.
// Returns an error if the input is not a valid expression.
func ParseExpr(input string) (ast.Expr, error) {
node, err := Parse(input)
if err != nil {
return nil, err
}
if expr, ok := node.(ast.Expr); ok {
return expr, nil
}
return nil, fmt.Errorf("parsed input is not an expression, got %T", node)
}

// ReaderParser parses consecutive ClassAds from a buffered reader without
Expand Down
Loading
Loading