summaryrefslogtreecommitdiff
path: root/parser/kind.go
diff options
context:
space:
mode:
authorMarc Vertes <marc.vertes@tendermint.com>2023-07-24 14:19:42 +0200
committerGitHub <noreply@github.com>2023-07-24 14:19:42 +0200
commitbf32256ff3014543ef5dda69c4ee1e94d01361fe (patch)
tree7ed1080462ed45864704a276eabaefa1fc299d8d /parser/kind.go
parentaa4cc2b45ebadf4cea16d1e27149e13669f3a5fc (diff)
parser: define all node kinds to make the parser multi-language (#3)
* parser: define all node kinds to make the parser multi-language Defining all AST node kinds in the parser is necessary to make the parser really multi-language. If a language requires a node kind not already present in parser/kind.go, it will be necessary to add it first here. Note that as long as a node kind subtree is structurally identical between languages, even if there are lexical and/or syntaxic differences, it can (and must) be shared amongst multiple language definitions. For example, an "if" statememt in shell script or in C code should give the same `IfStmt` at AST level. In order to let the parser deal with the various language syntaxes, and produce the right node kind and subtree, parser flags will be set in language definitions (see `Flags` field in `NodeSpec` struct). * lang/golang: use parser node kinds * vm0: remode dependency on language definition.
Diffstat (limited to 'parser/kind.go')
-rw-r--r--parser/kind.go52
1 files changed, 52 insertions, 0 deletions
diff --git a/parser/kind.go b/parser/kind.go
new file mode 100644
index 0000000..d570358
--- /dev/null
+++ b/parser/kind.go
@@ -0,0 +1,52 @@
+package parser
+
+import "fmt"
+
+type Kind int
+
+const (
+ Undefined = Kind(iota)
+ FuncDecl
+ CallExpr
+ IfStmt
+ StmtBloc
+ ReturnStmt
+ Ident
+ StringLit
+ NumberLit
+ ParBloc
+ DotOp
+ MulOp
+ AddOp
+ SubOp
+ AssignOp
+ DefOp
+ InfOp
+)
+
+var kindString = [...]string{
+ Undefined: "Undefined",
+ FuncDecl: "FuncDecl",
+ CallExpr: "CallExpr",
+ IfStmt: "IfStmt",
+ StmtBloc: "StmtBloc",
+ ReturnStmt: "ReturnStmt",
+ Ident: "Ident",
+ StringLit: "StringLit",
+ NumberLit: "NumberLit",
+ ParBloc: "ParBloc",
+ DotOp: "DotOp",
+ MulOp: "MulOp",
+ AddOp: "AddOP",
+ SubOp: "SubOp",
+ AssignOp: "AssignOp",
+ DefOp: "DefOp",
+ InfOp: "InfOp",
+}
+
+func (k Kind) String() string {
+ if int(k) < 0 || int(k) > len(kindString) {
+ return fmt.Sprintf("unknown kind %d", k)
+ }
+ return kindString[k]
+}