From a7fb3131fc576eb443b9c72cfc7fee2003b5cae3 Mon Sep 17 00:00:00 2001 From: Pete Vilter Date: Sat, 14 Dec 2024 23:56:49 -0500 Subject: [PATCH] TreeDL: basic scoping (#505) * local vars * var decls * scope parent * bind attrs * make grammar more compact --- .../languages/treeDL/example.txt | 12 ------ languageWorkbench/languages/treeDL/treeDL.dl | 42 +++++++++++++++++++ .../languages/treeDL/treeDL.grammar | 42 ++++++------------- 3 files changed, 54 insertions(+), 42 deletions(-) diff --git a/languageWorkbench/languages/treeDL/example.txt b/languageWorkbench/languages/treeDL/example.txt index 81e61f58..5dc3deeb 100644 --- a/languageWorkbench/languages/treeDL/example.txt +++ b/languageWorkbench/languages/treeDL/example.txt @@ -2,16 +2,4 @@ rule { raw_node = rawNode() id = raw_node.id node = Node.add(id=id) - rule { - next = Node() - raw_next = raw_node.next - next == raw_next - node.set(next=next) - } - rule { - prev = Node() - raw_prev = raw_node.prev - prev == raw_prev - node.set(prev=prev) - } } \ No newline at end of file diff --git a/languageWorkbench/languages/treeDL/treeDL.dl b/languageWorkbench/languages/treeDL/treeDL.dl index b89305a0..d2f538b0 100644 --- a/languageWorkbench/languages/treeDL/treeDL.dl +++ b/languageWorkbench/languages/treeDL/treeDL.dl @@ -1,3 +1,45 @@ +# Var + +scope.Var{scopeID: I, name: N, span: S, kind: K} :- + var.localVar{scopeID: I, name: N, span: S, kind: K}. +var.localVar{scopeID: Statement, name: N, span: S, kind: "var"} :- + ast.Statement{id: Statement} & + ast.Assign{id: Assign, parentID: Statement} & + ast.Expr{id: Expr, parentID: Assign} & + var.expr{id: Expr, name: N, span: S}. +var.expr{id: I, name: N, span: S} :- + var.memberAccess{id: I, name: N, span: S} | + var.bindAttr{id: I, name: N, span: S}. +var.memberAccess{id: Expr, name: N, span: span{from: Start, to: End}} :- + ast.Expr{id: Expr} & + ast.MemberAccess{id: MemberAccess, parentID: Expr, span: span{from: Start}} & + ast.Ident{parentID: MemberAccess, text: N, span: span{from: Start, to: End}}. +var.bindAttr{id: Expr, name: N, span: span{from: Start, to: End}} :- + ast.Expr{id: Expr} & + ast.Bind{id: Bind, parentID: Expr} & + ast.Attrs{id: Attrs, parentID: Bind} & + ast.Attr{id: Attr, parentID: Attrs, span: span{to: End}} & + ast.Ident{parentID: Attr, text: N, span: span{from: Start, to: End}}. # TODO: use 'key' + +# Defn + +scope.Defn{scopeID: I, span: S, name: N, kind: K, type: T} :- + defn.localVar{scopeID: I, span: S, name: N, kind: K, type: T}. + +defn.localVar{scopeID: Statement, span: S, name: N, kind: "var", type: "any"} :- + ast.Statement{id: Statement} & + ast.Assign{id: Assign, parentID: Statement} & + ast.Ident{parentID: Assign, span: S, text: N}. + +# Parent + +scope.Parent{parentID: First, childID: Second} :- + ast.Statement{id: First} & + ast.Statement{id: Second} & + astInternal.next{prev: First, next: Second}. + +# Highlight + hl.mapping{rule: "Ident", type: "ident"}. hl.mapping{rule: "NumberLit", type: "number"}. hl.mapping{rule: "BoolLit", type: "number"}. diff --git a/languageWorkbench/languages/treeDL/treeDL.grammar b/languageWorkbench/languages/treeDL/treeDL.grammar index f0f86fe1..9dd2f05f 100644 --- a/languageWorkbench/languages/treeDL/treeDL.grammar +++ b/languageWorkbench/languages/treeDL/treeDL.grammar @@ -1,24 +1,10 @@ main :- Rule. Program :- repSep(Rule, ws). -Rule :- [ - ruleKW, - ws, - "{", - ws, - Sequence, - ws, - "}" -]. +Rule :- [ruleKW, ws, "{", ws, Sequence, ws, "}"]. Sequence :- repSep(Statement, ws). Statement :- (Assign | Filter | Bind | Rule). Assign :- [Ident, ws, "=", ws, Expr]. -Filter :- [ - Ident, - ws, - InfixOp, - ws, - Ident -]. +Filter :- [Ident, ws, InfixOp, ws, Ident]. Expr :- ( MemberAccess | Literal | @@ -29,22 +15,21 @@ Expr :- ( MemberAccess :- repSep(Ident, "."). Call :- InfixCall. Get :- [Ident, Attrs]. -InfixCall :- [ - Ident, - ws, - InfixOp, - ws, - Ident -]. +InfixCall :- [Ident, ws, InfixOp, ws, Ident]. PrefixCall :- [Ident, Attrs]. Attrs :- ["(", repSep(Attr, CommaSpace), ")"]. -Attr :- [Ident, "=", Ident]. +Attr :- [key:Ident, "=", value:Ident]. InfixOp :- ("+" | "-" | "*" | "/" | "==" | "!=" | "<" | "<=" | ">" | ">="). Bind :- [Ident, ".", addKW, Attrs]. Literal :- (StringLit | NumberLit | BoolLit). +# keywords +ruleKW :- "rule". +addKW :- "add". +setKW :- "set". -Ident :- [alpha, repSep((alphaNum | "."), "")]. +# stdlib +Ident :- [alpha, repSep(alphaNum, "")]. StringLit :- ["\"", repSep(stringChar, ""), "\""]. NumberLit :- [("-" | ""), first:num, repSep(num, "")]. BoolLit :- ("true" | "false"). @@ -57,9 +42,6 @@ spaces :- repSep(" ", ""). placeholder :- "???". CommaSpace :- [",", ws]. -ruleKW :- "rule". -addKW :- "add". -setKW :- "set". - -commentChar :- ^'\n'. comment :- ["#", repSep(commentChar, "")]. +commentChar :- ^'\n'. +