1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
|
// Taken from https://github.com/circuitgraph/circuitgraph/blob/master/circuitgraph/parsing/verilog.lark
// Following https://www.verilog.com/VerilogBNF.html
// 1. Source Text
start: description*
?description: module
module: "module" name_of_module list_of_ports? ";" module_item* "endmodule"
?name_of_module: IDENTIFIER
list_of_ports: "(" port ("," port)* ")"
?port: IDENTIFIER
?module_item: input_declaration
| output_declaration
| net_declaration
| module_instantiation
| continuous_assign
// 2. Declarations
input_declaration: "input" list_of_variables ";"
output_declaration: "output" list_of_variables ";"
net_declaration: "wire" list_of_variables ";"
continuous_assign: "assign" list_of_assignments ";"
list_of_variables: IDENTIFIER ("," IDENTIFIER)*
list_of_assignments: assignment ("," assignment)*
// 3. Primitive Instances
// These are merged with module instantiations
// 4. Module Instantiations
module_instantiation: name_of_module module_instance ("," module_instance)* ";"
module_instance: name_of_instance "(" list_of_module_connections ")"
?name_of_instance: IDENTIFIER
list_of_module_connections: module_port_connection ("," module_port_connection)*
| named_port_connection ("," named_port_connection)*
module_port_connection: expression
named_port_connection: "." IDENTIFIER "(" expression ")"
// 5. Behavioral Statements
assignment: lvalue "=" expression
// 6. Specify Section
// 7. Expressions
?lvalue: identifier
expression: condition
?constant_value: constant_zero
| constant_one
| constant_x
constant_zero: "1'b0"
| "1'h0"
constant_one: "1'b1"
| "1'h1"
constant_x: "1'bx"
| "1'hx"
?condition : or
| ternary
?ternary: or "?" or ":" or
?or : xor
| or_gate
?or_gate: or "|" xor
?xor : and
| xor_gate
| xnor_gate
?xor_gate: xor "^" and
?xnor_gate: xor "~^" and
| xor "^~" and
?and : unary
| and_gate
?and_gate: and "&" unary
?unary : primary
| not_gate
not_gate: ( "!" | "~" ) primary
?primary : IDENTIFIER
| constant_value
| "(" or ")"
// 8. General
?identifier: IDENTIFIER
IDENTIFIER: CNAME
| ESCAPED_IDENTIFIER
// Lark
ESCAPED_IDENTIFIER: /\\([^\s]+)/
COMMENT: "//" /[^\n]*/ NEWLINE
NEWLINE: "\n"
MULTILINE_COMMENT: /\/\*(\*(?!\/)|[^*])*\*\//
%import common.CNAME
%import common.ESCAPED_STRING
%import common.WS
%ignore WS
%ignore COMMENT
%ignore MULTILINE_COMMENT
%ignore NEWLINE
|