Skip to content

Commit

Permalink
Convert more things to procedures
Browse files Browse the repository at this point in the history
  • Loading branch information
mickeynp committed Feb 26, 2024
1 parent 1f801c9 commit 8c0f024
Show file tree
Hide file tree
Showing 36 changed files with 3,286 additions and 2,744 deletions.
105 changes: 59 additions & 46 deletions build/build-relationships.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,18 @@ def build_all_node_types(rules):
for field, sub_rules in per_field_rules.items():
for sub_rule in sub_rules:
types.add(sub_rule)
# types.add(field)
return types


def process_rules(data):
def build_supertypes(data):
return [LispString(el) for el in data.get("supertypes", [])]


def process_grammar(data):
return build_supertypes(data)


def process_nodes(data):
rules = defaultdict(lambda: defaultdict(set))
for obj in data:
match_rule(obj, rules)
Expand All @@ -141,7 +148,7 @@ def make_rules_symbol(language, *rest):
return Symbol(f"combobulate-rules-{language}" + "".join(rest))


def generate_sexp(rules, inv_rules, all_node_types, language, source):
def generate_sexp(rules, inv_rules, all_node_types, supertypes, language):
l = []
for rule_name, rule_fields in rules.items():
# Ignore named fields and only generate the defaults?
Expand Down Expand Up @@ -181,10 +188,19 @@ def generate_sexp(rules, inv_rules, all_node_types, language, source):
"\n",
]
),
sexp(
[
Symbol("defconst"),
make_rules_symbol(language, "-supertypes"),
"\n",
Quote(sexp([n for n in sorted(supertypes)]) or []) or "nil",
"\n",
]
),
]


def load_node_types(source):
def load_json(source):
assert isinstance(source, Path), f"{source} must be a Path-like object."
try:
return json.loads(source.read_text())
Expand Down Expand Up @@ -229,7 +245,7 @@ def write_form(form, header: str | None = None, footer: str | None = None):
newline()

langs = []
for src, (form, inv_form, all_node_types_form) in forms:
for src, (form, inv_form, all_node_types_form, supertypes_form) in forms:
langs.append(src)
if not form:
log.error("Skipping %s as it is empty", src)
Expand All @@ -249,6 +265,11 @@ def write_form(form, header: str | None = None, footer: str | None = None):
header=f"All node types in {src}",
footer=f"All node types in {src}",
)
write_form(
supertypes_form,
header=f"All supertypes in {src}",
footer=f"All supertypes in {src}",
)
newline()
write_form(
sexp(
Expand Down Expand Up @@ -283,41 +304,19 @@ def make_alist(rules_symbol_extra):
)
)

write_form(
sexp(
[
Symbol("defconst"),
Symbol("combobulate-rules-alist"),
"\n",
make_alist(""),
]
),
header="Auto-generated alist of all languages and their production rules",
)
newline()
write_form(
sexp(
[
Symbol("defconst"),
Symbol("combobulate-rules-inverse-alist"),
"\n",
make_alist("-inverse"),
]
),
)
newline()
write_form(
sexp(
[
Symbol("defconst"),
Symbol("combobulate-rules-types-alist"),
"\n",
make_alist("-types"),
]
),
footer="Auto-generated alist of all languages and their production rules",
)
newline()
for rules_symbol_extra in ["", "-inverse", "-types", "-supertypes"]:
write_form(
sexp(
[
Symbol("defconst"),
Symbol(f"combobulate-rules{rules_symbol_extra}-alist"),
"\n",
make_alist(rules_symbol_extra),
]
),
)

newline()
write_form(
sexp(
[
Expand All @@ -328,11 +327,18 @@ def make_alist(rules_symbol_extra):
)


def parse_source(language, source):
log.info("Parsing language %s with node file %s", language, source)
data = load_node_types(source)
rules, inv_rules, all_node_types = process_rules(data)
return generate_sexp(rules, inv_rules, all_node_types, language, source)
def parse_source(language, nodes_fn, grammar_fn):
log.info(
"Parsing language %s with node file %s and grammar file %s",
language,
nodes_fn,
grammar_fn,
)
node_data = load_json(nodes_fn)
grammar_data = load_json(grammar_fn)
supertypes = process_grammar(grammar_data)
rules, inv_rules, all_node_types = process_nodes(node_data)
return generate_sexp(rules, inv_rules, all_node_types, supertypes, language)


def read_sources(sources_file: str) -> dict:
Expand Down Expand Up @@ -360,7 +366,14 @@ def main():
download_all_sources(sources)

for src, files in sources.items():
forms.append((src, parse_source(src, Path(f"{src}-nodes.json"))))
forms.append(
(
src,
parse_source(
src, Path(f"{src}-nodes.json"), Path(f"{src}-grammar.json")
),
)
)
write_elisp_file(forms)


Expand Down
1 change: 1 addition & 0 deletions build/tsx-grammar.json
Original file line number Diff line number Diff line change
Expand Up @@ -11429,3 +11429,4 @@
]
}


36 changes: 19 additions & 17 deletions combobulate-css.el
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,24 @@
'((:activation-nodes ((:nodes (all))) :selector (:choose node :match-children t))))

(setq combobulate-navigation-sibling-procedures
'((:activation-nodes
'(;; declarations' own property values should be siblings, but
;; not property_name as it's a child of declaration also,
;; and that'd mean the LHS and RHS are siblings of another,
;; which would be weird.
(:activation-nodes
((:nodes
((rule "feature_query")
(rule "arguments"))
:has-parent ("feature_query" "arguments")))
:selector (:choose parent
:match-children t))
(:activation-nodes
((:nodes
((exclude (rule "declaration") "property_name"))
:has-parent ("declaration")))
:selector (:choose parent
:match-children (:discard-rules ("comment" "property_name"))))
(:activation-nodes
((:nodes
((rule "block")
(rule "stylesheet"))
Expand All @@ -102,22 +119,7 @@
((:nodes
("declaration")
:has-parent ("block")))
:selector (:match-children (:discard-rules ("comment" "property_name"))))
;; declarations' own property values should be siblings, but
;; not property_name as it's a child of declaration also,
;; and that'd mean the LHS and RHS are siblings of another,
;; which would be weird.
(:activation-nodes
((:nodes
((exclude ((rule "declaration")) "property_name"))
:has-parent ("declaration")))
:selector (:match-children (:discard-rules ("comment" "property_name"))))
(:activation-nodes
((:nodes
((rule "feature_query")
(rule "arguments"))
:has-parent ("feature_query" "arguments")))
:selector (:match-children (:discard-rules ("comment"))))))
:selector (:match-children (:discard-rules ("comment" "property_name"))))))
(setq combobulate-navigation-defun-procedures
'((:activation-nodes ((:nodes (exclude (all) "declaration")))))))

Expand Down
12 changes: 7 additions & 5 deletions combobulate-display.el
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,13 @@
(with-navigation-nodes (:nodes (seq-difference
combobulate-navigation-default-nodes
combobulate-display-ignored-node-types))
(when (member (combobulate-node-type node) combobulate-display-ignored-node-types)
(setq node (combobulate--get-nearest-navigable-node)))
(when node
(save-excursion
(combobulate-display-draw-tree-1 (combobulate-display-create-locus node) node)))))
(cond
((member (combobulate-node-type node) combobulate-display-ignored-node-types)
(setq node (combobulate--get-nearest-navigable-node)
hl-node nil))
((not node) (setq node (combobulate--get-nearest-navigable-node)
hl-node (combobulate--get-nearest-navigable-node))))
(combobulate-display-draw-tree-1 (combobulate-display-create-locus node) hl-node)))


(defun combobulate-display-create-locus (start-node)
Expand Down
Loading

0 comments on commit 8c0f024

Please sign in to comment.