Skip to content

Commit

Permalink
Simplify direct nodes.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandreLachanceGit committed Sep 22, 2023
1 parent 641d80c commit 5acfedd
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 42 deletions.
3 changes: 1 addition & 2 deletions examples/basic.p4
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ header ipv4_t {
bit<16> hdrChecksum;
ip4Addr_t srcAddr;
ip4Addr_t dstAddr;
ethernet_t test3;
}

struct metadata {
Expand All @@ -51,7 +50,7 @@ parser MyParser(packet_in packet,
out headers hdr,
inout metadata meta,
inout standard_metadata_t standard_metadata) {
bit<4> test = hdr.ipv4.test3.etherType;
bit<4> test = hdr.ethernet;
ethernet_t test2 = hdr.ethernet;

state start {
Expand Down
2 changes: 1 addition & 1 deletion ls_framework/src/language_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub enum TreesitterNodeQuery {

#[derive(Debug, Deserialize, Clone)]
pub enum DirectOrRule {
Direct(NodeKind),
Direct(String),
Rule(String),
}

Expand Down
2 changes: 1 addition & 1 deletion ls_framework/src/metadata/ast/rules_translator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ impl RulesTranslator {

current_node_id.append(
self.new_node(
node_kind.clone(),
NodeKind::Node(node_kind.clone()),
&target_node,
Symbol::None,
child.highlight_type.clone(),
Expand Down
76 changes: 38 additions & 38 deletions p4_lsf/language_def/rules.ron
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ LanguageDefinition (
(name: "Table", completion_type: Class, highlight_type: Class),
],
global_ast_rules: [
(query: Kind("line_comment"), rule: Direct(Node("Comment")), highlight_type: Comment),
(query: Kind("block_comment"), rule: Direct(Node("Comment")), highlight_type: Comment),
(query: Kind("line_comment"), rule: Direct("Comment"), highlight_type: Comment),
(query: Kind("block_comment"), rule: Direct("Comment"), highlight_type: Comment),
],
ast_rules: [
Rule(
Expand All @@ -74,7 +74,7 @@ LanguageDefinition (
children: [
(query: Field("annotation"), rule: Rule("Annotation")),
(query: Field("type"), rule: Rule("Type")),
(query: Field("name"), rule: Direct(Node("Name"))),
(query: Field("name"), rule: Direct("Name")),
(query: Field("value"), rule: Rule("Expression")),
]
),
Expand All @@ -84,7 +84,7 @@ LanguageDefinition (
symbol: Init(type: "Function", name_node: "Name"),
children: [
(query: Field("annotation"), rule: Rule("Annotation")),
(query: Path([Field("declaration"), Field("name")]), rule: Direct(Node("Name"))),
(query: Path([Field("declaration"), Field("name")]), rule: Direct("Name")),
(query: Path([Field("declaration"), Field("parameters")]), rule: Rule("Parameters")),
(query: Field("body"), rule: Rule("Body")),
]
Expand All @@ -95,7 +95,7 @@ LanguageDefinition (
symbol: Init(type: "Function", name_node: "Name"),
children: [
(query: Field("annotation"), rule: Rule("Annotation")),
(query: Path([Field("declaration"), Field("name")]), rule: Direct(Node("Name"))),
(query: Path([Field("declaration"), Field("name")]), rule: Direct("Name")),
(query: Path([Field("declaration"), Field("parameters")]), rule: Rule("Parameters")),
(query: Field("body"), rule: Rule("Body")),
]
Expand All @@ -106,7 +106,7 @@ LanguageDefinition (
children: [
(query: Kind("annotation"), rule: Rule("Annotation")),
(query: Kind("type_ref"), rule: Rule("Type")),
(query: Kind("name"), rule: Direct(Node("Name"))),
(query: Kind("name"), rule: Direct("Name")),
(query: Kind("argument_list"), rule: Rule("Args")),
]
),
Expand All @@ -129,7 +129,7 @@ LanguageDefinition (
children: [
(query: Field("annotation"), rule: Rule("Annotation")),
(query: Field("type"), rule: Rule("Type")),
(query: Field("name"), rule: Direct(Node("Name"))),
(query: Field("name"), rule: Direct("Name")),
]
),
Rule(
Expand All @@ -138,7 +138,7 @@ LanguageDefinition (
is_scope: true,
children: [
(query: Field("annotation"), rule: Rule("Annotation")),
(query: Field("name"), rule: Direct(Node("Name"))),
(query: Field("name"), rule: Direct("Name")),
(query: Field("field_list"), rule: Rule("FieldList"))
// maybe type parameters
]
Expand All @@ -149,7 +149,7 @@ LanguageDefinition (
is_scope: true,
children: [
(query: Field("annotation"), rule: Rule("Annotation")),
(query: Field("name"), rule: Direct(Node("Name"))),
(query: Field("name"), rule: Direct("Name")),
(query: Field("field_list"), rule: Rule("FieldList"))
// maybe type parameters
]
Expand All @@ -160,7 +160,7 @@ LanguageDefinition (
is_scope: true,
children: [
(query: Field("annotation"), rule: Rule("Annotation")),
(query: Field("name"), rule: Direct(Node("Name"))),
(query: Field("name"), rule: Direct("Name")),
(query: Field("field_list"), rule: Rule("FieldList"))
// maybe type parameters
]
Expand All @@ -171,7 +171,7 @@ LanguageDefinition (
children: [
(query: Field("annotation"), rule: Rule("Annotation")),
(query: Field("type"), rule: Rule("Type")),
(query: Field("name"), rule: Direct(Node("Name"))),
(query: Field("name"), rule: Direct("Name")),
(query: Kind("identifier_list"), rule: Rule("OptionList")),
(query: Kind("specified_identifier_list"), rule: Rule("SpecifiedOptionList"))
// maybe type parameters
Expand All @@ -182,7 +182,7 @@ LanguageDefinition (
symbol: Init(type: "Function", name_node: "Name"),
children: [
(query: Field("annotation"), rule: Rule("Annotation")),
(query: Field("name"), rule: Direct(Node("Name"))),
(query: Field("name"), rule: Direct("Name")),
(query: Field("parameters"), rule: Rule("Parameters"))
// maybe type parameters
]
Expand All @@ -192,7 +192,7 @@ LanguageDefinition (
symbol: Init(type: "Function", name_node: "Name"),
children: [
(query: Field("annotation"), rule: Rule("Annotation")),
(query: Field("name"), rule: Direct(Node("Name"))),
(query: Field("name"), rule: Direct("Name")),
(query: Field("parameters"), rule: Rule("Parameters"))
// maybe type parameters
]
Expand All @@ -202,7 +202,7 @@ LanguageDefinition (
symbol: Init(type: "Function", name_node: "Name"),
children: [
(query: Field("annotation"), rule: Rule("Annotation")),
(query: Field("name"), rule: Direct(Node("Name"))),
(query: Field("name"), rule: Direct("Name")),
(query: Field("parameters"), rule: Rule("Parameters"))
// maybe type parameters
]
Expand All @@ -219,25 +219,25 @@ LanguageDefinition (
children: [
(query: Field("annotation"), rule: Rule("Annotation")),
(query: Field("type"), rule: Rule("Type")),
(query: Field("name"), rule: Direct(Node("Name"))),
(query: Field("name"), rule: Direct("Name")),
]
),
Rule(
node_name: "OptionList",
children: [
(query: Kind("name"), rule: Direct(Node("Option"))),
(query: Kind("name"), rule: Direct("Option")),
]
),
Rule(
node_name: "SpecifiedOptionList",
children: [
(query: Kind("name"), rule: Direct(Node("Option"))),
(query: Kind("name"), rule: Direct("Option")),
]
),
Rule(
node_name: "SpecifiedOption",
children: [
(query: Kind("name"), rule: Direct(Node("Option"))),
(query: Kind("name"), rule: Direct("Option")),
(query: Kind("initializer"), rule: Rule("Expression")),
]
),
Expand All @@ -247,7 +247,7 @@ LanguageDefinition (
is_scope: true,
children: [
(query: Field("annotation"), rule: Rule("Annotation")),
(query: Field("name"), rule: Direct(Node("Name"))),
(query: Field("name"), rule: Direct("Name")),
(query: Field("parameters"), rule: Rule("Parameters")),
(query: Field("block"), rule: Rule("Block")),
],
Expand All @@ -272,7 +272,7 @@ LanguageDefinition (
symbol: Init(type: "Function", name_node: "Name"),
children: [
(query: Field("annotation"), rule: Rule("Annotation")),
(query: Field("name"), rule: Direct(Node("Name"))),
(query: Field("name"), rule: Direct("Name")),
// maybe type parameters
(query: Field("function"), rule: Rule("FunctionPrototype")),
(query: Field("method"), rule: Rule("MethodList")),
Expand All @@ -282,7 +282,7 @@ LanguageDefinition (
node_name: "FunctionPrototype",
children: [
(query: Kind("type_or_void"), rule: Rule("Type")),
(query: Kind("name"), rule: Direct(Node("Name"))),
(query: Kind("name"), rule: Direct("Name")),
(query: Kind("parameter_list"), rule: Rule("Parameters")),
]
),
Expand All @@ -304,7 +304,7 @@ LanguageDefinition (
Rule(
node_name: "Options",
children: [
(query: Kind("name"), rule: Direct(Node("Option"))),
(query: Kind("name"), rule: Direct("Option")),
]
),
Rule(
Expand All @@ -329,9 +329,9 @@ LanguageDefinition (
node_name: "Parameter",
symbol: Init(type: "Parameter", name_node: "Name", type_node: "Type"),
children: [
(query: Field("direction"), rule: Direct(Node("Direction")), highlight_type: EnumMember),
(query: Field("direction"), rule: Direct("Direction"), highlight_type: EnumMember),
(query: Field("type"), rule: Rule("Type")),
(query: Field("name"), rule: Direct(Node("Name"))),
(query: Field("name"), rule: Direct("Name")),
]
),
Rule(
Expand All @@ -358,10 +358,10 @@ LanguageDefinition (
(query: Kind("assignment_or_method_call_statement"), rule: Rule("AssignmentOrMethodCall")),
(query: Kind("direct_application"), rule: Rule("DirectApplication")),
(query: Kind("conditional_statement"), rule: Rule("Conditional")),
(query: Kind("empty_statement"), rule: Direct(Node("EmptyStatement"))),
(query: Kind("empty_statement"), rule: Direct("EmptyStatement")),
(query: Kind("block_statement"), rule: Rule("Block")),
(query: Kind("parser_block_statement"), rule: Rule("Block")),
(query: Kind("exit_statement"), rule: Direct(Node("ExitStatement"))),
(query: Kind("exit_statement"), rule: Direct("ExitStatement")),
(query: Kind("return_statement"), rule: Rule("Return")),
(query: Kind("switch_statement"), rule: Rule("Switch")),
(query: Kind("transition_statement"), rule: Rule("Transition")),
Expand Down Expand Up @@ -400,7 +400,7 @@ LanguageDefinition (
children: [
(query: Field("annotation"), rule: Rule("Annotation")),
(query: Field("type"), rule: Rule("Type")),
(query: Field("name"), rule: Direct(Node("Name"))),
(query: Field("name"), rule: Direct("Name")),
(query: Field("value"), rule: Rule("Expression")),
]
),
Expand Down Expand Up @@ -429,7 +429,7 @@ LanguageDefinition (
Rule(
node_name: "AssignmentOrMethodCall",
children: [
(query: Field("name"), rule: Direct(Node("Name"))),
(query: Field("name"), rule: Direct("Name")),
(query: Field("expression"), rule: Rule("Expression")),
(query: Field("parameters"), rule: Rule("Args")),
]
Expand All @@ -439,7 +439,7 @@ LanguageDefinition (
symbol: Init(type: "Table", name_node: "Name"),
children: [
(query: Field("annotation"), rule: Rule("Annotation")),
(query: Field("name"), rule: Direct(Node("Name"))),
(query: Field("name"), rule: Direct("Name")),
(query: Field("table"), rule: Rule("Table")),
]
),
Expand All @@ -461,7 +461,7 @@ LanguageDefinition (
node_name: "KeyElement",
children: [
(query: Field("expression"), rule: Rule("Expression")),
(query: Field("name"), rule: Direct(Node("Name"))),
(query: Field("name"), rule: Direct("Name")),
]
),
Rule(
Expand All @@ -479,15 +479,15 @@ LanguageDefinition (
Rule(
node_name: "NameTable",
children: [
(query: Field("name"), rule: Direct(Node("Name"))),
(query: Field("name"), rule: Direct("Name")),
(query: Field("expression"), rule: Rule("Expression")),
]
),
Rule(
node_name: "ParserState",
children: [
(query: Field("annotation"), rule: Rule("Annotation")),
(query: Field("name"), rule: Direct(Node("Name"))),
(query: Field("name"), rule: Direct("Name")),
(query: Field("body"), rule: Rule("Block")),
]
),
Expand All @@ -496,7 +496,7 @@ LanguageDefinition (
children: [
(query: Field("annotation"), rule: Rule("Annotation")),
(query: Field("type"), rule: Rule("Type")),
(query: Field("name"), rule: Direct(Node("Name"))),
(query: Field("name"), rule: Direct("Name")),
(query: Field("expression"), rule: Rule("Expression")),
]
),
Expand All @@ -505,7 +505,7 @@ LanguageDefinition (
children: [
// Change tree-sitter-p4 field names to combine both ?
(query: Field("name"), rule: Rule("NameUsage")),
(query: Field("specialized"), rule: Direct(Node("Name"))),
(query: Field("specialized"), rule: Direct("Name")),

(query: Field("args"), rule: Rule("Args")),
]
Expand Down Expand Up @@ -544,15 +544,15 @@ LanguageDefinition (
node_name: "Expression",
symbol: Expression,
children: [
(query: Kind("integer"), rule: Direct(Node("Integer")), highlight_type: Number),
(query: Kind("string"), rule: Direct(Node("String")), highlight_type: String),
(query: Kind("bool"), rule: Direct(Node("Bool")), highlight_type: EnumMember),
(query: Kind("integer"), rule: Direct("Integer"), highlight_type: Number),
(query: Kind("string"), rule: Direct("String"), highlight_type: String),
(query: Kind("bool"), rule: Direct("Bool"), highlight_type: EnumMember),
(query: Kind("non_type_name"), rule: Rule("NameUsage")),
(query: Kind("named_type"), rule: Rule("NameUsage")),
(query: Kind("type_name"), rule: Rule("NameUsage")),
(query: Kind("expression"), rule: Rule("Expression")),
(query: Kind("member"), rule: Rule("Member")),
(query: Field("operator"), rule: Direct(Node("Operator"))),
(query: Field("operator"), rule: Direct("Operator")),
]
),
Rule(
Expand Down

0 comments on commit 5acfedd

Please sign in to comment.