This commit is contained in:
2024-08-10 19:48:14 +02:00
parent 459c7f47cd
commit aeec086b95
3 changed files with 36 additions and 44 deletions
+12 -37
View File
@@ -2,59 +2,34 @@ package parser
import (
"fmt"
"github.com/wyst-lang/wyst/tokenizer"
)
type inner interface {
isInner()
}
type Rule struct {
Name string
Inner inner
Inner []Rule
}
func (t Rule) String() string {
return fmt.Sprintf("AstRule(%s)", t.Name)
}
type Rules struct {
Value []Rule
}
func (v Rules) isInner() {}
type Token struct {
Value tokenizer.Rule
}
func (v Token) isInner() {}
type String struct {
Value string
}
func (v String) isInner() {}
type AstNode struct {
Rule Rule
Value string
Inner []AstNode
}
func (t AstNode) String() string {
return fmt.Sprintf("AstNode(\n rule=%s,\n value=%s,\n inner=%v\n)", t.Rule, t.Value, t.Inner)
return fmt.Sprintf("AstNode(\n rule=%v,\n inner=%v,\n)", t.Rule, t.Inner)
}
func (t Rule) String() string {
return t.Name
}
func NewRule(name string, inner []Rule) Rule {
return Rule{name, inner}
}
// Ported rules from the tokenizer
var (
IDENTIFIER = Rule{"IDENTIFIER", Token{tokenizer.IDENTIFIER}}
IDENTIFIER = NewRule("IDENTIFIER", []Rule{})
VAR_DEF = NewRule("VAR_DEF", []Rule{IDENTIFIER, IDENTIFIER})
)
var (
TOP = Rule{"TOP", Rules{
[]Rule{
IDENTIFIER,
}}}
RULE_SET = []Rule{IDENTIFIER, VAR_DEF}
)