Skip to content

Commit

Permalink
TreeDL: basic scoping (#505)
Browse files Browse the repository at this point in the history
* local vars

* var decls

* scope parent

* bind attrs

* make grammar more compact
  • Loading branch information
vilterp authored Dec 15, 2024
1 parent 4d6ef31 commit a7fb313
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 42 deletions.
12 changes: 0 additions & 12 deletions languageWorkbench/languages/treeDL/example.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
42 changes: 42 additions & 0 deletions languageWorkbench/languages/treeDL/treeDL.dl
Original file line number Diff line number Diff line change
@@ -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"}.
Expand Down
42 changes: 12 additions & 30 deletions languageWorkbench/languages/treeDL/treeDL.grammar
Original file line number Diff line number Diff line change
@@ -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 |
Expand All @@ -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").
Expand All @@ -57,9 +42,6 @@ spaces :- repSep(" ", "").
placeholder :- "???".
CommaSpace :- [",", ws].

ruleKW :- "rule".
addKW :- "add".
setKW :- "set".

commentChar :- ^'\n'.
comment :- ["#", repSep(commentChar, "")].
commentChar :- ^'\n'.

0 comments on commit a7fb313

Please sign in to comment.