[ruby/yarp] Rename child_nodes to fields

https://github.com/ruby/yarp/commit/715fce9264
This commit is contained in:
Kevin Newton 2023-08-30 16:09:51 -04:00 committed by git
parent f83070816d
commit ff024150e7
12 changed files with 625 additions and 622 deletions

View File

@ -361,7 +361,7 @@ flags:
comment: "o - only interpolates values into the regular expression once"
nodes:
- name: AliasNode
child_nodes:
fields:
- name: new_name
type: node
- name: old_name
@ -374,7 +374,7 @@ nodes:
alias foo bar
^^^^^^^^^^^^^
- name: AlternationPatternNode
child_nodes:
fields:
- name: left
type: node
- name: right
@ -387,7 +387,7 @@ nodes:
foo => bar | baz
^^^^^^^^^
- name: AndNode
child_nodes:
fields:
- name: left
type: node
- name: right
@ -400,7 +400,7 @@ nodes:
left and right
^^^^^^^^^^^^^^
- name: ArgumentsNode
child_nodes:
fields:
- name: arguments
type: node[]
comment: |
@ -409,7 +409,7 @@ nodes:
return foo, bar, baz
^^^^^^^^^^^^^
- name: ArrayNode
child_nodes:
fields:
- name: elements
type: node[]
- name: opening_loc
@ -423,7 +423,7 @@ nodes:
[1, 2, 3]
^^^^^^^^^
- name: ArrayPatternNode
child_nodes:
fields:
- name: constant
type: node?
- name: requireds
@ -454,7 +454,7 @@ nodes:
foo in Bar[1, 2, 3]
^^^^^^^^^^^^^^^^^^^
- name: AssocNode
child_nodes:
fields:
- name: key
type: node
- name: value
@ -467,7 +467,7 @@ nodes:
{ a => b }
^^^^^^
- name: AssocSplatNode
child_nodes:
fields:
- name: value
type: node?
- name: operator_loc
@ -484,7 +484,7 @@ nodes:
$'
^^
- name: BeginNode
child_nodes:
fields:
- name: begin_keyword_loc
type: location?
- name: statements
@ -510,7 +510,7 @@ nodes:
end
^^^^^
- name: BlockArgumentNode
child_nodes:
fields:
- name: expression
type: node?
- name: operator_loc
@ -521,7 +521,7 @@ nodes:
bar(&args)
^^^^^^^^^^
- name: BlockNode
child_nodes:
fields:
- name: locals
type: constant[]
- name: parameters
@ -539,7 +539,7 @@ nodes:
[1, 2, 3].each { |i| puts x }
^^^^^^^^^^^^^^
- name: BlockParameterNode
child_nodes:
fields:
- name: name_loc
type: location?
- name: operator_loc
@ -551,7 +551,7 @@ nodes:
^^
end
- name: BlockParametersNode
child_nodes:
fields:
- name: parameters
type: node?
kind: ParametersNode
@ -571,7 +571,7 @@ nodes:
^^^^^^^^^^^^^^^^^
end
- name: BreakNode
child_nodes:
fields:
- name: arguments
type: node?
kind: ArgumentsNode
@ -583,7 +583,7 @@ nodes:
break foo
^^^^^^^^^
- name: CallNode
child_nodes:
fields:
- name: receiver
type: node?
- name: operator_loc
@ -626,7 +626,7 @@ nodes:
foo&.bar
^^^^^^^^
- name: CallOperatorAndWriteNode
child_nodes:
fields:
- name: target
type: node
kind: CallNode
@ -640,7 +640,7 @@ nodes:
foo.bar &&= value
^^^^^^^^^^^^^^^^^
- name: CallOperatorOrWriteNode
child_nodes:
fields:
- name: target
type: node
kind: CallNode
@ -654,7 +654,7 @@ nodes:
foo.bar ||= value
^^^^^^^^^^^^^^^^^
- name: CallOperatorWriteNode
child_nodes:
fields:
- name: target
type: node
kind: CallNode
@ -670,7 +670,7 @@ nodes:
foo.bar += baz
^^^^^^^^^^^^^^
- name: CapturePatternNode
child_nodes:
fields:
- name: value
type: node
- name: target
@ -683,7 +683,7 @@ nodes:
foo => [bar => baz]
^^^^^^^^^^^^
- name: CaseNode
child_nodes:
fields:
- name: predicate
type: node?
- name: conditions
@ -703,7 +703,7 @@ nodes:
when false
end
- name: ClassNode
child_nodes:
fields:
- name: locals
type: constant[]
- name: class_keyword_loc
@ -726,7 +726,7 @@ nodes:
class Foo end
^^^^^^^^^^^^^
- name: ClassVariableAndWriteNode
child_nodes:
fields:
- name: name
type: constant
- name: name_loc
@ -741,7 +741,7 @@ nodes:
@@target &&= value
^^^^^^^^^^^^^^^^
- name: ClassVariableOperatorWriteNode
child_nodes:
fields:
- name: name
type: constant
- name: name_loc
@ -758,7 +758,7 @@ nodes:
@@target += value
^^^^^^^^^^^^^^^^^
- name: ClassVariableOrWriteNode
child_nodes:
fields:
- name: name
type: constant
- name: name_loc
@ -791,7 +791,7 @@ nodes:
@@foo, @@bar = baz
^^^^^ ^^^^^
- name: ClassVariableWriteNode
child_nodes:
fields:
- name: name
type: constant
- name: name_loc
@ -806,7 +806,7 @@ nodes:
@@foo = 1
^^^^^^^^^
- name: ConstantAndWriteNode
child_nodes:
fields:
- name: name_loc
type: location
- name: operator_loc
@ -819,7 +819,7 @@ nodes:
Target &&= value
^^^^^^^^^^^^^^^^
- name: ConstantOperatorWriteNode
child_nodes:
fields:
- name: name_loc
type: location
- name: operator_loc
@ -834,7 +834,7 @@ nodes:
Target += value
^^^^^^^^^^^^^^^
- name: ConstantOrWriteNode
child_nodes:
fields:
- name: name_loc
type: location
- name: operator_loc
@ -847,7 +847,7 @@ nodes:
Target ||= value
^^^^^^^^^^^^^^^^
- name: ConstantPathAndWriteNode
child_nodes:
fields:
- name: target
type: node
kind: ConstantPathNode
@ -861,7 +861,7 @@ nodes:
Parent::Child &&= value
^^^^^^^^^^^^^^^^^^^^^^^
- name: ConstantPathNode
child_nodes:
fields:
- name: parent
type: node?
- name: child
@ -874,7 +874,7 @@ nodes:
Foo::Bar
^^^^^^^^
- name: ConstantPathOperatorWriteNode
child_nodes:
fields:
- name: target
type: node
kind: ConstantPathNode
@ -890,7 +890,7 @@ nodes:
Parent::Child += value
^^^^^^^^^^^^^^^^^^^^^^
- name: ConstantPathOrWriteNode
child_nodes:
fields:
- name: target
type: node
kind: ConstantPathNode
@ -904,7 +904,7 @@ nodes:
Parent::Child ||= value
^^^^^^^^^^^^^^^^^^^^^^^
- name: ConstantPathTargetNode
child_nodes:
fields:
- name: parent
type: node?
- name: child
@ -917,7 +917,7 @@ nodes:
Foo::Foo, Bar::Bar = baz
^^^^^^^^ ^^^^^^^^
- name: ConstantPathWriteNode
child_nodes:
fields:
- name: target
type: node
kind: ConstantPathNode
@ -949,7 +949,7 @@ nodes:
Foo, Bar = baz
^^^ ^^^
- name: ConstantWriteNode
child_nodes:
fields:
- name: name_loc
type: location
- name: value
@ -962,7 +962,7 @@ nodes:
Foo = 1
^^^^^^^
- name: DefNode
child_nodes:
fields:
- name: name_loc
type: location
- name: receiver
@ -993,7 +993,7 @@ nodes:
end
^^^^^^^^^^
- name: DefinedNode
child_nodes:
fields:
- name: lparen_loc
type: location?
- name: value
@ -1008,7 +1008,7 @@ nodes:
defined?(a)
^^^^^^^^^^^
- name: ElseNode
child_nodes:
fields:
- name: else_keyword_loc
type: location
- name: statements
@ -1022,7 +1022,7 @@ nodes:
if a then b else c end
^^^^^^^^^^
- name: EmbeddedStatementsNode
child_nodes:
fields:
- name: opening_loc
type: location
- name: statements
@ -1036,7 +1036,7 @@ nodes:
"foo #{bar}"
^^^^^^
- name: EmbeddedVariableNode
child_nodes:
fields:
- name: operator_loc
type: location
- name: variable
@ -1047,7 +1047,7 @@ nodes:
"foo #@bar"
^^^^^
- name: EnsureNode
child_nodes:
fields:
- name: ensure_keyword_loc
type: location
- name: statements
@ -1071,7 +1071,7 @@ nodes:
false
^^^^^
- name: FindPatternNode
child_nodes:
fields:
- name: constant
type: node?
- name: left
@ -1096,7 +1096,7 @@ nodes:
foo in Foo(*bar, baz, *qux)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
- name: FlipFlopNode
child_nodes:
fields:
- name: left
type: node?
- name: right
@ -1118,7 +1118,7 @@ nodes:
1.0
^^^
- name: ForNode
child_nodes:
fields:
- name: index
type: node
- name: collection
@ -1155,7 +1155,7 @@ nodes:
^^^
end
- name: ForwardingSuperNode
child_nodes:
fields:
- name: block
type: node?
kind: BlockNode
@ -1165,7 +1165,7 @@ nodes:
super
^^^^^
- name: GlobalVariableAndWriteNode
child_nodes:
fields:
- name: name_loc
type: location
- name: operator_loc
@ -1178,7 +1178,7 @@ nodes:
$target &&= value
^^^^^^^^^^^^^^^^^
- name: GlobalVariableOperatorWriteNode
child_nodes:
fields:
- name: name_loc
type: location
- name: operator_loc
@ -1193,7 +1193,7 @@ nodes:
$target += value
^^^^^^^^^^^^^^^^
- name: GlobalVariableOrWriteNode
child_nodes:
fields:
- name: name_loc
type: location
- name: operator_loc
@ -1218,7 +1218,7 @@ nodes:
$foo, $bar = baz
^^^^ ^^^^
- name: GlobalVariableWriteNode
child_nodes:
fields:
- name: name_loc
type: location
- name: operator_loc
@ -1231,7 +1231,7 @@ nodes:
$foo = 1
^^^^^^^^
- name: HashNode
child_nodes:
fields:
- name: opening_loc
type: location
- name: elements
@ -1244,7 +1244,7 @@ nodes:
{ a => b }
^^^^^^^^^^
- name: HashPatternNode
child_nodes:
fields:
- name: constant
type: node?
- name: assocs
@ -1264,7 +1264,7 @@ nodes:
foo => { a: 1, b: 2, **c }
^^^^^^^^^^^^^^^^^^^
- name: IfNode
child_nodes:
fields:
- name: if_keyword_loc
type: location?
- name: predicate
@ -1286,7 +1286,7 @@ nodes:
if foo then bar end
^^^^^^^^^^^^^^^^^^^
- name: ImaginaryNode
child_nodes:
fields:
- name: numeric
type: node
comment: |
@ -1295,7 +1295,7 @@ nodes:
1.0i
^^^^
- name: InNode
child_nodes:
fields:
- name: pattern
type: node
- name: statements
@ -1311,7 +1311,7 @@ nodes:
case a; in b then c end
^^^^^^^^^^^
- name: InstanceVariableAndWriteNode
child_nodes:
fields:
- name: name
type: constant
- name: name_loc
@ -1326,7 +1326,7 @@ nodes:
@target &&= value
^^^^^^^^^^^^^^^^^
- name: InstanceVariableOperatorWriteNode
child_nodes:
fields:
- name: name
type: constant
- name: name_loc
@ -1343,7 +1343,7 @@ nodes:
@target += value
^^^^^^^^^^^^^^^^
- name: InstanceVariableOrWriteNode
child_nodes:
fields:
- name: name
type: constant
- name: name_loc
@ -1358,7 +1358,7 @@ nodes:
@target ||= value
^^^^^^^^^^^^^^^^^
- name: InstanceVariableReadNode
child_nodes:
fields:
- name: name
type: constant
comment: |
@ -1367,7 +1367,7 @@ nodes:
@foo
^^^^
- name: InstanceVariableTargetNode
child_nodes:
fields:
- name: name
type: constant
comment: |
@ -1376,7 +1376,7 @@ nodes:
@foo, @bar = baz
^^^^ ^^^^
- name: InstanceVariableWriteNode
child_nodes:
fields:
- name: name
type: constant
- name: name_loc
@ -1397,7 +1397,7 @@ nodes:
1
^
- name: InterpolatedRegularExpressionNode
child_nodes:
fields:
- name: opening_loc
type: location
- name: parts
@ -1414,7 +1414,7 @@ nodes:
/foo #{bar} baz/
^^^^^^^^^^^^^^^^
- name: InterpolatedStringNode
child_nodes:
fields:
- name: opening_loc
type: location?
- name: parts
@ -1428,7 +1428,7 @@ nodes:
"foo #{bar} baz"
^^^^^^^^^^^^^^^^
- name: InterpolatedSymbolNode
child_nodes:
fields:
- name: opening_loc
type: location?
- name: parts
@ -1442,7 +1442,7 @@ nodes:
:"foo #{bar} baz"
^^^^^^^^^^^^^^^^^
- name: InterpolatedXStringNode
child_nodes:
fields:
- name: opening_loc
type: location
- name: parts
@ -1456,7 +1456,7 @@ nodes:
`foo #{bar} baz`
^^^^^^^^^^^^^^^^
- name: KeywordHashNode
child_nodes:
fields:
- name: elements
type: node[]
comment: |
@ -1465,7 +1465,7 @@ nodes:
foo(a: b)
^^^^
- name: KeywordParameterNode
child_nodes:
fields:
- name: name_loc
type: location
- name: value
@ -1481,7 +1481,7 @@ nodes:
^^^^
end
- name: KeywordRestParameterNode
child_nodes:
fields:
- name: operator_loc
type: location
- name: name_loc
@ -1493,7 +1493,7 @@ nodes:
^^^
end
- name: LambdaNode
child_nodes:
fields:
- name: locals
type: constant[]
- name: operator_loc
@ -1513,7 +1513,7 @@ nodes:
->(value) { value * 2 }
^^^^^^^^^^^^^^^^^^^^^^^
- name: LocalVariableAndWriteNode
child_nodes:
fields:
- name: name_loc
type: location
- name: operator_loc
@ -1530,7 +1530,7 @@ nodes:
target &&= value
^^^^^^^^^^^^^^^^
- name: LocalVariableOperatorWriteNode
child_nodes:
fields:
- name: name_loc
type: location
- name: operator_loc
@ -1549,7 +1549,7 @@ nodes:
target += value
^^^^^^^^^^^^^^^
- name: LocalVariableOrWriteNode
child_nodes:
fields:
- name: name_loc
type: location
- name: operator_loc
@ -1566,7 +1566,7 @@ nodes:
target ||= value
^^^^^^^^^^^^^^^^
- name: LocalVariableReadNode
child_nodes:
fields:
- name: name
type: constant
- name: depth
@ -1579,7 +1579,7 @@ nodes:
foo
^^^
- name: LocalVariableTargetNode
child_nodes:
fields:
- name: name
type: constant
- name: depth
@ -1590,7 +1590,7 @@ nodes:
foo, bar = baz
^^^ ^^^
- name: LocalVariableWriteNode
child_nodes:
fields:
- name: name
type: constant
- name: depth
@ -1607,7 +1607,7 @@ nodes:
foo = 1
^^^^^^^
- name: MatchPredicateNode
child_nodes:
fields:
- name: value
type: node
- name: pattern
@ -1620,7 +1620,7 @@ nodes:
foo in bar
^^^^^^^^^^
- name: MatchRequiredNode
child_nodes:
fields:
- name: value
type: node
- name: pattern
@ -1637,7 +1637,7 @@ nodes:
Represents a node that is missing from the source and results in a syntax
error.
- name: ModuleNode
child_nodes:
fields:
- name: locals
type: constant[]
- name: module_keyword_loc
@ -1656,7 +1656,7 @@ nodes:
module Foo end
^^^^^^^^^^^^^^
- name: MultiWriteNode
child_nodes:
fields:
- name: targets
type: node[]
- name: operator_loc
@ -1673,7 +1673,7 @@ nodes:
a, b, c = 1, 2, 3
^^^^^^^^^^^^^^^^^
- name: NextNode
child_nodes:
fields:
- name: arguments
type: node?
kind: ArgumentsNode
@ -1691,7 +1691,7 @@ nodes:
nil
^^^
- name: NoKeywordsParameterNode
child_nodes:
fields:
- name: operator_loc
type: location
- name: keyword_loc
@ -1709,7 +1709,7 @@ nodes:
$1
^^
- name: OptionalParameterNode
child_nodes:
fields:
- name: name
type: constant
- name: name_loc
@ -1725,7 +1725,7 @@ nodes:
^^^^^
end
- name: OrNode
child_nodes:
fields:
- name: left
type: node
- name: right
@ -1738,7 +1738,7 @@ nodes:
left or right
^^^^^^^^^^^^^
- name: ParametersNode
child_nodes:
fields:
- name: requireds
type: node[]
- name: optionals
@ -1762,7 +1762,7 @@ nodes:
^^^^^^^
end
- name: ParenthesesNode
child_nodes:
fields:
- name: body
type: node?
- name: opening_loc
@ -1776,7 +1776,7 @@ nodes:
(10 + 34)
^^^^^^^^^
- name: PinnedExpressionNode
child_nodes:
fields:
- name: expression
type: node
- name: operator_loc
@ -1792,7 +1792,7 @@ nodes:
foo in ^(bar)
^^^^^^
- name: PinnedVariableNode
child_nodes:
fields:
- name: variable
type: node
- name: operator_loc
@ -1804,7 +1804,7 @@ nodes:
foo in ^bar
^^^^
- name: PostExecutionNode
child_nodes:
fields:
- name: statements
type: node?
kind: StatementsNode
@ -1820,7 +1820,7 @@ nodes:
END { foo }
^^^^^^^^^^^
- name: PreExecutionNode
child_nodes:
fields:
- name: statements
type: node?
kind: StatementsNode
@ -1836,7 +1836,7 @@ nodes:
BEGIN { foo }
^^^^^^^^^^^^^
- name: ProgramNode
child_nodes:
fields:
- name: locals
type: constant[]
- name: statements
@ -1844,7 +1844,7 @@ nodes:
kind: StatementsNode
comment: The top level node of any parse tree.
- name: RangeNode
child_nodes:
fields:
- name: left
type: node?
- name: right
@ -1863,7 +1863,7 @@ nodes:
c if a =~ /left/ ... b =~ /right/
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- name: RationalNode
child_nodes:
fields:
- name: numeric
type: node
comment: |
@ -1878,7 +1878,7 @@ nodes:
redo
^^^^
- name: RegularExpressionNode
child_nodes:
fields:
- name: opening_loc
type: location
- name: content_loc
@ -1896,7 +1896,7 @@ nodes:
/foo/i
^^^^^^
- name: RequiredDestructuredParameterNode
child_nodes:
fields:
- name: parameters
type: node[]
- name: opening_loc
@ -1910,7 +1910,7 @@ nodes:
^^^^^^^^^^
end
- name: RequiredParameterNode
child_nodes:
fields:
- name: name
type: constant
comment: |
@ -1920,7 +1920,7 @@ nodes:
^
end
- name: RescueModifierNode
child_nodes:
fields:
- name: expression
type: node
- name: keyword_loc
@ -1934,7 +1934,7 @@ nodes:
foo rescue nil
^^^^^^^^^^^^^^
- name: RescueNode
child_nodes:
fields:
- name: keyword_loc
type: location
- name: exceptions
@ -1961,7 +1961,7 @@ nodes:
`Foo, *splat, Bar` are in the `exceptions` field.
`ex` is in the `exception` field.
- name: RestParameterNode
child_nodes:
fields:
- name: operator_loc
type: location
- name: name_loc
@ -1979,7 +1979,7 @@ nodes:
retry
^^^^^
- name: ReturnNode
child_nodes:
fields:
- name: keyword_loc
type: location
- name: arguments
@ -1997,7 +1997,7 @@ nodes:
self
^^^^
- name: SingletonClassNode
child_nodes:
fields:
- name: locals
type: constant[]
- name: class_keyword_loc
@ -2022,7 +2022,7 @@ nodes:
__ENCODING__
^^^^^^^^^^^^
- name: SourceFileNode
child_nodes:
fields:
- name: filepath
type: string
comment: |
@ -2037,7 +2037,7 @@ nodes:
__LINE__
^^^^^^^^
- name: SplatNode
child_nodes:
fields:
- name: operator_loc
type: location
- name: expression
@ -2048,7 +2048,7 @@ nodes:
[*a]
^^
- name: StatementsNode
child_nodes:
fields:
- name: body
type: node[]
comment: |
@ -2057,7 +2057,7 @@ nodes:
foo; bar; baz
^^^^^^^^^^^^^
- name: StringConcatNode
child_nodes:
fields:
- name: left
type: node
- name: right
@ -2068,7 +2068,7 @@ nodes:
"foo" "bar"
^^^^^^^^^^^
- name: StringNode
child_nodes:
fields:
- name: opening_loc
type: location?
- name: content_loc
@ -2090,7 +2090,7 @@ nodes:
"foo #{bar} baz"
^^^^ ^^^^
- name: SuperNode
child_nodes:
fields:
- name: keyword_loc
type: location
- name: lparen_loc
@ -2112,7 +2112,7 @@ nodes:
super foo, bar
^^^^^^^^^^^^^^
- name: SymbolNode
child_nodes:
fields:
- name: opening_loc
type: location?
- name: value_loc
@ -2136,7 +2136,7 @@ nodes:
true
^^^^
- name: UndefNode
child_nodes:
fields:
- name: names
type: node[]
- name: keyword_loc
@ -2147,7 +2147,7 @@ nodes:
undef :foo, :bar, :baz
^^^^^^^^^^^^^^^^^^^^^^
- name: UnlessNode
child_nodes:
fields:
- name: keyword_loc
type: location
- name: predicate
@ -2170,7 +2170,7 @@ nodes:
unless foo then bar end
^^^^^^^^^^^^^^^^^^^^^^^
- name: UntilNode
child_nodes:
fields:
- name: keyword_loc
type: location
- name: closing_loc
@ -2193,7 +2193,7 @@ nodes:
until foo do bar end
^^^^^^^^^^^^^^^^^^^^
- name: WhenNode
child_nodes:
fields:
- name: keyword_loc
type: location
- name: conditions
@ -2209,7 +2209,7 @@ nodes:
^^^^^^^^^
end
- name: WhileNode
child_nodes:
fields:
- name: keyword_loc
type: location
- name: closing_loc
@ -2232,7 +2232,7 @@ nodes:
while foo do bar end
^^^^^^^^^^^^^^^^^^^^
- name: XStringNode
child_nodes:
fields:
- name: opening_loc
type: location
- name: content_loc
@ -2247,7 +2247,7 @@ nodes:
`foo`
^^^^^
- name: YieldNode
child_nodes:
fields:
- name: keyword_loc
type: location
- name: lparen_loc

View File

@ -106,17 +106,17 @@ yp_ast_new(yp_parser_t *parser, yp_node_t *node, rb_encoding *encoding) {
switch (YP_NODE_TYPE(node)) {
<%- nodes.each do |node| -%>
<%- if node.params.any? { |param| [NodeParam, OptionalNodeParam, NodeListParam].include?(param.class) } -%>
<%- if node.fields.any? { |field| [YARP::NodeField, YARP::OptionalNodeField, YARP::NodeListField].include?(field.class) } -%>
#line <%= __LINE__ + 1 %> "<%= File.basename(__FILE__) %>"
case <%= node.type %>: {
yp_<%= node.human %>_t *cast = (yp_<%= node.human %>_t *) node;
<%- node.params.each do |param| -%>
<%- case param -%>
<%- when NodeParam, OptionalNodeParam -%>
yp_node_stack_push(&node_stack, (yp_node_t *) cast-><%= param.name %>);
<%- when NodeListParam -%>
for (size_t index = 0; index < cast-><%= param.name %>.size; index++) {
yp_node_stack_push(&node_stack, (yp_node_t *) cast-><%= param.name %>.nodes[index]);
<%- node.fields.each do |field| -%>
<%- case field -%>
<%- when YARP::NodeField, YARP::OptionalNodeField -%>
yp_node_stack_push(&node_stack, (yp_node_t *) cast-><%= field.name %>);
<%- when YARP::NodeListField -%>
for (size_t index = 0; index < cast-><%= field.name %>.size; index++) {
yp_node_stack_push(&node_stack, (yp_node_t *) cast-><%= field.name %>.nodes[index]);
}
<%- end -%>
<%- end -%>
@ -135,53 +135,53 @@ yp_ast_new(yp_parser_t *parser, yp_node_t *node, rb_encoding *encoding) {
<%- nodes.each do |node| -%>
#line <%= __LINE__ + 1 %> "<%= File.basename(__FILE__) %>"
case <%= node.type %>: {
<%- if node.params.any? { |param| ![NodeParam, OptionalNodeParam].include?(param.class) } -%>
<%- if node.fields.any? { |field| ![YARP::NodeField, YARP::OptionalNodeField].include?(field.class) } -%>
yp_<%= node.human %>_t *cast = (yp_<%= node.human %>_t *) node;
<%- end -%>
VALUE argv[<%= node.params.length + 1 %>];
<%- node.params.each_with_index do |param, index| -%>
VALUE argv[<%= node.fields.length + 1 %>];
<%- node.fields.each_with_index do |field, index| -%>
// <%= param.name %>
<%- case param -%>
<%- when NodeParam, OptionalNodeParam -%>
// <%= field.name %>
<%- case field -%>
<%- when YARP::NodeField, YARP::OptionalNodeField -%>
argv[<%= index %>] = rb_ary_pop(value_stack);
<%- when NodeListParam -%>
argv[<%= index %>] = rb_ary_new_capa(cast-><%= param.name %>.size);
for (size_t index = 0; index < cast-><%= param.name %>.size; index++) {
<%- when YARP::NodeListField -%>
argv[<%= index %>] = rb_ary_new_capa(cast-><%= field.name %>.size);
for (size_t index = 0; index < cast-><%= field.name %>.size; index++) {
rb_ary_push(argv[<%= index %>], rb_ary_pop(value_stack));
}
<%- when StringParam -%>
argv[<%= index %>] = yp_string_new(&cast-><%= param.name %>, encoding);
<%- when LocationListParam -%>
argv[<%= index %>] = rb_ary_new_capa(cast-><%= param.name %>.size);
for (size_t index = 0; index < cast-><%= param.name %>.size; index++) {
yp_location_t location = cast-><%= param.name %>.locations[index];
<%- when YARP::StringField -%>
argv[<%= index %>] = yp_string_new(&cast-><%= field.name %>, encoding);
<%- when YARP::LocationListField -%>
argv[<%= index %>] = rb_ary_new_capa(cast-><%= field.name %>.size);
for (size_t index = 0; index < cast-><%= field.name %>.size; index++) {
yp_location_t location = cast-><%= field.name %>.locations[index];
rb_ary_push(argv[<%= index %>], yp_location_new(parser, location.start, location.end, source));
}
<%- when ConstantParam -%>
argv[<%= index %>] = rb_id2sym(constants[cast-><%= param.name %> - 1]);
<%- when ConstantListParam -%>
argv[<%= index %>] = rb_ary_new_capa(cast-><%= param.name %>.size);
for (size_t index = 0; index < cast-><%= param.name %>.size; index++) {
rb_ary_push(argv[<%= index %>], rb_id2sym(constants[cast-><%= param.name %>.ids[index] - 1]));
<%- when YARP::ConstantField -%>
argv[<%= index %>] = rb_id2sym(constants[cast-><%= field.name %> - 1]);
<%- when YARP::ConstantListField -%>
argv[<%= index %>] = rb_ary_new_capa(cast-><%= field.name %>.size);
for (size_t index = 0; index < cast-><%= field.name %>.size; index++) {
rb_ary_push(argv[<%= index %>], rb_id2sym(constants[cast-><%= field.name %>.ids[index] - 1]));
}
<%- when LocationParam -%>
argv[<%= index %>] = yp_location_new(parser, cast-><%= param.name %>.start, cast-><%= param.name %>.end, source);
<%- when OptionalLocationParam -%>
argv[<%= index %>] = cast-><%= param.name %>.start == NULL ? Qnil : yp_location_new(parser, cast-><%= param.name %>.start, cast-><%= param.name %>.end, source);
<%- when UInt32Param -%>
argv[<%= index %>] = ULONG2NUM(cast-><%= param.name %>);
<%- when FlagsParam -%>
argv[<%= index %>] = ULONG2NUM(node->flags >> <%= COMMON_FLAGS %>);
<%- when YARP::LocationField -%>
argv[<%= index %>] = yp_location_new(parser, cast-><%= field.name %>.start, cast-><%= field.name %>.end, source);
<%- when YARP::OptionalLocationField -%>
argv[<%= index %>] = cast-><%= field.name %>.start == NULL ? Qnil : yp_location_new(parser, cast-><%= field.name %>.start, cast-><%= field.name %>.end, source);
<%- when YARP::UInt32Field -%>
argv[<%= index %>] = ULONG2NUM(cast-><%= field.name %>);
<%- when YARP::FlagsField -%>
argv[<%= index %>] = ULONG2NUM(node->flags >> <%= YARP::COMMON_FLAGS %>);
<%- else -%>
<%- raise -%>
<%- end -%>
<%- end -%>
// location
argv[<%= node.params.length %>] = yp_location_new(parser, node->location.start, node->location.end, source);
argv[<%= node.fields.length %>] = yp_location_new(parser, node->location.start, node->location.end, source);
rb_ary_push(value_stack, rb_class_new_instance(<%= node.params.length + 1 %>, argv, rb_cYARP<%= node.name %>));
rb_ary_push(value_stack, rb_class_new_instance(<%= node.fields.length + 1 %>, argv, rb_cYARP<%= node.name %>));
break;
}
<%- end -%>

View File

@ -83,17 +83,17 @@ typedef struct yp_node {
// <%= node.name %>
typedef struct yp_<%= node.human %> {
yp_node_t base;
<%- node.params.grep_v(FlagsParam).each do |param| -%>
<%= case param
when NodeParam, OptionalNodeParam then "struct #{param.c_type} *#{param.name}"
when NodeListParam then "struct yp_node_list #{param.name}"
when LocationListParam then "yp_location_list_t #{param.name}"
when ConstantParam then "yp_constant_id_t #{param.name}"
when ConstantListParam then "yp_constant_id_list_t #{param.name}"
when StringParam then "yp_string_t #{param.name}"
when LocationParam, OptionalLocationParam then "yp_location_t #{param.name}"
when UInt32Param then "uint32_t #{param.name}"
else raise param.class.name
<%- node.fields.grep_v(YARP::FlagsField).each do |field| -%>
<%= case field
when YARP::NodeField, YARP::OptionalNodeField then "struct #{field.c_type} *#{field.name}"
when YARP::NodeListField then "struct yp_node_list #{field.name}"
when YARP::LocationListField then "yp_location_list_t #{field.name}"
when YARP::ConstantField then "yp_constant_id_t #{field.name}"
when YARP::ConstantListField then "yp_constant_id_list_t #{field.name}"
when YARP::StringField then "yp_string_t #{field.name}"
when YARP::LocationField, YARP::OptionalLocationField then "yp_location_t #{field.name}"
when YARP::UInt32Field then "uint32_t #{field.name}"
else raise field.class.name
end
%>;
<%- end -%>
@ -103,7 +103,7 @@ typedef struct yp_<%= node.human %> {
// <%= flag.name %>
typedef enum {
<%- flag.values.each.with_index(COMMON_FLAGS) do |value, index| -%>
<%- flag.values.each.with_index(YARP::COMMON_FLAGS) do |value, index| -%>
YP_<%= flag.human.upcase %>_<%= value.name %> = 1 << <%= index %>,
<%- end -%>
} yp_<%= flag.human %>_t;

View File

@ -257,19 +257,19 @@ public class Loader {
case <%= index + 1 %>:
<%-
params = node.needs_serialized_length? ? ["buffer.getInt()"] : []
params.concat node.params.map { |param|
case param
when NodeParam then "#{param.java_cast}loadNode()"
when OptionalNodeParam then "#{param.java_cast}loadOptionalNode()"
when StringParam then "loadString()"
when NodeListParam then "loadNodes()"
when LocationListParam then "loadLocations()"
when ConstantParam then "loadConstant()"
when ConstantListParam then "loadConstants()"
when LocationParam then "loadLocation()"
when OptionalLocationParam then "loadOptionalLocation()"
when UInt32Param then "loadVarInt()"
when FlagsParam then "loadFlags()"
params.concat node.fields.map { |field|
case field
when YARP::NodeField then "#{field.java_cast}loadNode()"
when YARP::OptionalNodeField then "#{field.java_cast}loadOptionalNode()"
when YARP::StringField then "loadString()"
when YARP::NodeListField then "loadNodes()"
when YARP::LocationListField then "loadLocations()"
when YARP::ConstantField then "loadConstant()"
when YARP::ConstantListField then "loadConstants()"
when YARP::LocationField then "loadLocation()"
when YARP::OptionalLocationField then "loadOptionalLocation()"
when YARP::UInt32Field then "loadVarInt()"
when YARP::FlagsField then "loadFlags()"
else raise
end
}

View File

@ -189,13 +189,13 @@ public abstract class Nodes {
<%- if node.needs_serialized_length? -%>
public final int serializedLength;
<%- end -%>
<%- node.params.each do |param| -%>
public final <%= param.java_type %> <%= param.name %>;<%= ' // optional' if param.class.name.start_with?('Optional') %>
<%- node.fields.each do |field| -%>
public final <%= field.java_type %> <%= field.name %>;<%= ' // optional' if field.class.name.start_with?('Optional') %>
<%- end -%>
<%-
params = node.needs_serialized_length? ? ["int serializedLength"] : []
params.concat node.params.map { "#{_1.java_type} #{_1.name}" }
params.concat node.fields.map { "#{_1.java_type} #{_1.name}" }
params.concat ["int startOffset", "int length"]
-%>
public <%=node.name -%>(<%= params.join(", ") %>) {
@ -203,17 +203,17 @@ public abstract class Nodes {
<%- if node.needs_serialized_length? -%>
this.serializedLength = serializedLength;
<%- end -%>
<%- node.params.each do |param| -%>
this.<%= param.name %> = <%= param.name %>;
<%- node.fields.each do |field| -%>
this.<%= field.name %> = <%= field.name %>;
<%- end -%>
}
<%# methods for flags -%>
<%- node.params.each do |param| -%>
<%- if param.is_a?(FlagsParam) -%>
<%- flags.find { |flag| flag.name == param.kind }.tap { raise "Expected to find #{param.kind}" unless _1 }.values.each do |value| -%>
<%- node.fields.each do |field| -%>
<%- if field.is_a?(YARP::FlagsField) -%>
<%- flags.find { |flag| flag.name == field.kind }.tap { raise "Expected to find #{field.kind}" unless _1 }.values.each do |value| -%>
public boolean is<%= value.camelcase %>() {
return <%= param.kind %>.is<%= value.camelcase %>(this.<%= param.name %>);
return <%= field.kind %>.is<%= value.camelcase %>(this.<%= field.name %>);
}
<%- end -%>
<%- end -%>
@ -229,52 +229,52 @@ public abstract class Nodes {
@Override
public void setNewLineFlag(Source source, boolean[] newlineMarked) {
<%- param = node.params.find { |p| p.name == node.newline } or raise node.newline -%>
<%- case param -%>
<%- when SingleNodeParam -%>
this.<%= param.name %>.setNewLineFlag(source, newlineMarked);
<%- when NodeListParam -%>
Node first = this.<%= param.name %>.length > 0 ? this.<%= param.name %>[0] : null;
<%- field = node.fields.find { |f| f.name == node.newline } or raise node.newline -%>
<%- case field -%>
<%- when YARP::NodeField, YARP::OptionalNodeField -%>
this.<%= field.name %>.setNewLineFlag(source, newlineMarked);
<%- when YARP::NodeListField -%>
Node first = this.<%= field.name %>.length > 0 ? this.<%= field.name %>[0] : null;
if (first != null) {
first.setNewLineFlag(source, newlineMarked);
}
<%- else raise param.class.name -%>
<%- else raise field.class.name -%>
<%- end -%>
}
<%- end -%>
public <T> void visitChildNodes(AbstractNodeVisitor<T> visitor) {
<%- node.params.each do |param| -%>
<%- case param -%>
<%- when NodeListParam -%>
for (Nodes.Node child : this.<%= param.name %>) {
<%- node.fields.each do |field| -%>
<%- case field -%>
<%- when YARP::NodeListField -%>
for (Nodes.Node child : this.<%= field.name %>) {
child.accept(visitor);
}
<%- when NodeParam -%>
this.<%= param.name %>.accept(visitor);
<%- when OptionalNodeParam -%>
if (this.<%= param.name %> != null) {
this.<%= param.name %>.accept(visitor);
<%- when YARP::NodeField -%>
this.<%= field.name %>.accept(visitor);
<%- when YARP::OptionalNodeField -%>
if (this.<%= field.name %> != null) {
this.<%= field.name %>.accept(visitor);
}
<%- end -%>
<%- end -%>
}
public Node[] childNodes() {
<%- if node.params.none?(NodeListParam) and node.params.none?(SingleNodeParam) -%>
<%- if node.fields.none?(YARP::NodeListField) and node.fields.none?(YARP::NodeKindField) -%>
return EMPTY_ARRAY;
<%- elsif node.params.one?(NodeListParam) and node.params.none?(SingleNodeParam) -%>
return this.<%= node.params.grep(NodeListParam).first.name %>;
<%- elsif node.params.none?(NodeListParam) -%>
return new Node[] { <%= node.params.grep(SingleNodeParam).map { "this.#{_1.name}" }.join(', ') %> };
<%- elsif node.fields.one?(YARP::NodeListField) and node.fields.none?(YARP::NodeKindField) -%>
return this.<%= node.fields.grep(YARP::NodeListField).first.name %>;
<%- elsif node.fields.none?(YARP::NodeListField) -%>
return new Node[] { <%= node.fields.grep(YARP::NodeKindField).map { "this.#{_1.name}" }.join(', ') %> };
<%- else -%>
ArrayList<Node> childNodes = new ArrayList<>();
<%- node.params.each do |param| -%>
<%- case param -%>
<%- when SingleNodeParam -%>
childNodes.add(this.<%= param.name %>);
<%- when NodeListParam -%>
childNodes.addAll(Arrays.asList(this.<%= param.name %>));
<%- node.fields.each do |field| -%>
<%- case field -%>
<%- when YARP::NodeField, YARP::OptionalNodeField -%>
childNodes.add(this.<%= field.name %>);
<%- when YARP::NodeListField -%>
childNodes.addAll(Arrays.asList(this.<%= field.name %>));
<%- end -%>
<%- end -%>
return childNodes.toArray(EMPTY_ARRAY);

View File

@ -7,9 +7,9 @@ module YARP
<%= "\n" if index != 0 -%>
# Copy a <%= node.name %> node
def visit_<%= node.human %>(node)
<%- params = node.params.select { |param| [NodeParam, OptionalNodeParam, NodeListParam].include?(param.class) } -%>
<%- if params.any? -%>
node.copy(<%= params.map { |param| "#{param.name}: #{param.is_a?(NodeListParam) ? "visit_all" : "visit"}(node.#{param.name})" }.join(", ") %>)
<%- fields = node.fields.select { |field| [YARP::NodeField, YARP::OptionalNodeField, YARP::NodeListField].include?(field.class) } -%>
<%- if fields.any? -%>
node.copy(<%= fields.map { |field| "#{field.name}: #{field.is_a?(YARP::NodeListField) ? "visit_all" : "visit"}(node.#{field.name})" }.join(", ") %>)
<%- else -%>
node.copy
<%- end -%>

View File

@ -1,15 +1,15 @@
module YARP
<%- nodes.each do |node| -%>
<%= "#{node.comment.split("\n").map { |line| line.empty? ? "#" : "# #{line}" }.join("\n ")}\n " if node.comment %>class <%= node.name -%> < Node
<%- node.params.each do |param| -%>
# attr_reader <%= param.name %>: <%= param.rbs_class %>
attr_reader :<%= param.name %>
<%- node.fields.each do |field| -%>
# attr_reader <%= field.name %>: <%= field.rbs_class %>
attr_reader :<%= field.name %>
<%- end -%>
# def initialize: (<%= (node.params.map { |param| "#{param.name}: #{param.rbs_class}" } + ["location: Location"]).join(", ") %>) -> void
def initialize(<%= (node.params.map(&:name) + ["location"]).join(", ") %>)
<%- node.params.each do |param| -%>
@<%= param.name %> = <%= param.name %>
# def initialize: (<%= (node.fields.map { |field| "#{field.name}: #{field.rbs_class}" } + ["location: Location"]).join(", ") %>) -> void
def initialize(<%= (node.fields.map(&:name) + ["location"]).join(", ") %>)
<%- node.fields.each do |field| -%>
@<%= field.name %> = <%= field.name %>
<%- end -%>
@location = location
end
@ -26,24 +26,24 @@ module YARP
<%- elsif node.newline.is_a?(String) -%>
def set_newline_flag(newline_marked)
<%- param = node.params.find { |p| p.name == node.newline } or raise node.newline -%>
<%- case param -%>
<%- when SingleNodeParam -%>
<%= param.name %>.set_newline_flag(newline_marked)
<%- when NodeListParam -%>
first = <%= param.name %>.first
<%- field = node.fields.find { |f| f.name == node.newline } or raise node.newline -%>
<%- case field -%>
<%- when YARP::NodeField, YARP::OptionalNodeField -%>
<%= field.name %>.set_newline_flag(newline_marked)
<%- when YARP::NodeListField -%>
first = <%= field.name %>.first
first.set_newline_flag(newline_marked) if first
<%- else raise param.class.name -%>
<%- else raise field.class.name -%>
<%- end -%>
end
<%- end -%>
# def child_nodes: () -> Array[nil | Node]
def child_nodes
[<%= node.params.map { |param|
case param
when SingleNodeParam then param.name
when NodeListParam then "*#{param.name}"
[<%= node.fields.map { |field|
case field
when YARP::NodeField, YARP::OptionalNodeField then field.name
when YARP::NodeListField then "*#{field.name}"
end
}.compact.join(", ") %>]
end
@ -51,7 +51,7 @@ module YARP
# def copy: (**params) -> <%= node.name %>
def copy(**params)
<%= node.name %>.new(
<%- (node.params.map(&:name) + ["location"]).map do |name| -%>
<%- (node.fields.map(&:name) + ["location"]).map do |name| -%>
params.fetch(:<%= name %>) { <%= name %> },
<%- end -%>
)
@ -62,32 +62,32 @@ module YARP
# def deconstruct_keys: (keys: Array[Symbol]) -> Hash[Symbol, nil | Node | Array[Node] | String | Token | Array[Token] | Location]
def deconstruct_keys(keys)
{ <%= (node.params.map { |param| "#{param.name}: #{param.name}" } + ["location: location"]).join(", ") %> }
{ <%= (node.fields.map { |field| "#{field.name}: #{field.name}" } + ["location: location"]).join(", ") %> }
end
<%- node.params.each do |param| -%>
<%- case param -%>
<%- when LocationParam -%>
<%- raise unless param.name.end_with?("_loc") -%>
<%- next if node.params.any? { |other| other.name == param.name.delete_suffix("_loc") } -%>
<%- node.fields.each do |field| -%>
<%- case field -%>
<%- when YARP::LocationField -%>
<%- raise unless field.name.end_with?("_loc") -%>
<%- next if node.fields.any? { |other| other.name == field.name.delete_suffix("_loc") } -%>
# def <%= param.name.delete_suffix("_loc") %>: () -> String
def <%= param.name.delete_suffix("_loc") %>
<%= param.name %>.slice
# def <%= field.name.delete_suffix("_loc") %>: () -> String
def <%= field.name.delete_suffix("_loc") %>
<%= field.name %>.slice
end
<%- when OptionalLocationParam -%>
<%- raise unless param.name.end_with?("_loc") -%>
<%- next if node.params.any? { |other| other.name == param.name.delete_suffix("_loc") } -%>
<%- when YARP::OptionalLocationField -%>
<%- raise unless field.name.end_with?("_loc") -%>
<%- next if node.fields.any? { |other| other.name == field.name.delete_suffix("_loc") } -%>
# def <%= param.name.delete_suffix("_loc") %>: () -> String?
def <%= param.name.delete_suffix("_loc") %>
<%= param.name %>&.slice
# def <%= field.name.delete_suffix("_loc") %>: () -> String?
def <%= field.name.delete_suffix("_loc") %>
<%= field.name %>&.slice
end
<%- when FlagsParam -%>
<%- flags.find { |flag| flag.name == param.kind }.tap { |flag| raise "Expected to find #{param.kind}" unless flag }.values.each do |value| -%>
<%- when YARP::FlagsField -%>
<%- flags.find { |flag| flag.name == field.kind }.tap { |flag| raise "Expected to find #{field.kind}" unless flag }.values.each do |value| -%>
# def <%= value.name.downcase %>?: () -> bool
def <%= value.name.downcase %>?
<%= param.name %>.anybits?(<%= param.kind %>::<%= value.name %>)
<%= field.name %>.anybits?(<%= field.kind %>::<%= value.name %>)
end
<%- end -%>
<%- end -%>
@ -123,8 +123,8 @@ module YARP
<%- nodes.each do |node| -%>
# Create a new <%= node.name %> node
def <%= node.name %>(<%= (node.params.map(&:name) + ["location = Location()"]).join(", ") %>)
<%= node.name %>.new(<%= (node.params.map(&:name) + ["location"]).join(", ") %>)
def <%= node.name %>(<%= (node.fields.map(&:name) + ["location = Location()"]).join(", ") %>)
<%= node.name %>.new(<%= (node.fields.map(&:name) + ["location"]).join(", ") %>)
end
<%- end -%>
end

View File

@ -179,18 +179,18 @@ module YARP
<%- if node.needs_serialized_length? -%>
load_serialized_length
<%- end -%>
<%= node.name %>.new(<%= (node.params.map { |param|
case param
when NodeParam then "load_node"
when OptionalNodeParam then "load_optional_node"
when StringParam then "load_string"
when NodeListParam then "Array.new(load_varint) { load_node }"
when LocationListParam then "Array.new(load_varint) { load_location }"
when ConstantParam then "load_constant"
when ConstantListParam then "Array.new(load_varint) { load_constant }"
when LocationParam then "load_location"
when OptionalLocationParam then "load_optional_location"
when UInt32Param, FlagsParam then "load_varint"
<%= node.name %>.new(<%= (node.fields.map { |field|
case field
when YARP::NodeField then "load_node"
when YARP::OptionalNodeField then "load_optional_node"
when YARP::StringField then "load_string"
when YARP::NodeListField then "Array.new(load_varint) { load_node }"
when YARP::LocationListField then "Array.new(load_varint) { load_location }"
when YARP::ConstantField then "load_constant"
when YARP::ConstantListField then "Array.new(load_varint) { load_constant }"
when YARP::LocationField then "load_location"
when YARP::OptionalLocationField then "load_optional_location"
when YARP::UInt32Field, YARP::FlagsField then "load_varint"
else raise
end
} + ["location"]).join(", ") -%>)

View File

@ -79,26 +79,26 @@ yp_node_destroy(yp_parser_t *parser, yp_node_t *node) {
<%- nodes.each do |node| -%>
#line <%= __LINE__ + 1 %> "<%= File.basename(__FILE__) %>"
case <%= node.type %>: {
<%- if node.params.any? { |param| ![LocationParam, OptionalLocationParam, UInt32Param, FlagsParam, ConstantParam].include?(param.class) } -%>
<%- if node.fields.any? { |field| ![YARP::LocationField, YARP::OptionalLocationField, YARP::UInt32Field, YARP::FlagsField, YARP::ConstantField].include?(field.class) } -%>
yp_<%= node.human %>_t *cast = (yp_<%= node.human %>_t *) node;
<%- end -%>
<%- node.params.each do |param| -%>
<%- case param -%>
<%- when LocationParam, OptionalLocationParam, UInt32Param, FlagsParam, ConstantParam -%>
<%- when NodeParam -%>
yp_node_destroy(parser, (yp_node_t *)cast-><%= param.name %>);
<%- when OptionalNodeParam -%>
if (cast-><%= param.name %> != NULL) {
yp_node_destroy(parser, (yp_node_t *)cast-><%= param.name %>);
<%- node.fields.each do |field| -%>
<%- case field -%>
<%- when YARP::LocationField, YARP::OptionalLocationField, YARP::UInt32Field, YARP::FlagsField, YARP::ConstantField -%>
<%- when YARP::NodeField -%>
yp_node_destroy(parser, (yp_node_t *)cast-><%= field.name %>);
<%- when YARP::OptionalNodeField -%>
if (cast-><%= field.name %> != NULL) {
yp_node_destroy(parser, (yp_node_t *)cast-><%= field.name %>);
}
<%- when StringParam -%>
yp_string_free(&cast-><%= param.name %>);
<%- when NodeListParam -%>
yp_node_list_free(parser, &cast-><%= param.name %>);
<%- when LocationListParam -%>
yp_location_list_free(&cast-><%= param.name %>);
<%- when ConstantListParam -%>
yp_constant_id_list_free(&cast-><%= param.name %>);
<%- when YARP::StringField -%>
yp_string_free(&cast-><%= field.name %>);
<%- when YARP::NodeListField -%>
yp_node_list_free(parser, &cast-><%= field.name %>);
<%- when YARP::LocationListField -%>
yp_location_list_free(&cast-><%= field.name %>);
<%- when YARP::ConstantListField -%>
yp_constant_id_list_free(&cast-><%= field.name %>);
<%- else -%>
<%- raise -%>
<%- end -%>
@ -128,23 +128,23 @@ yp_node_memsize_node(yp_node_t *node, yp_memsize_t *memsize) {
case <%= node.type %>: {
yp_<%= node.human %>_t *cast = (yp_<%= node.human %>_t *) node;
memsize->memsize += sizeof(*cast);
<%- node.params.each do |param| -%>
<%- case param -%>
<%- when ConstantParam, UInt32Param, FlagsParam, LocationParam, OptionalLocationParam -%>
<%- when NodeParam -%>
yp_node_memsize_node((yp_node_t *)cast-><%= param.name %>, memsize);
<%- when OptionalNodeParam -%>
if (cast-><%= param.name %> != NULL) {
yp_node_memsize_node((yp_node_t *)cast-><%= param.name %>, memsize);
<%- node.fields.each do |field| -%>
<%- case field -%>
<%- when YARP::ConstantField, YARP::UInt32Field, YARP::FlagsField, YARP::LocationField, YARP::OptionalLocationField -%>
<%- when YARP::NodeField -%>
yp_node_memsize_node((yp_node_t *)cast-><%= field.name %>, memsize);
<%- when YARP::OptionalNodeField -%>
if (cast-><%= field.name %> != NULL) {
yp_node_memsize_node((yp_node_t *)cast-><%= field.name %>, memsize);
}
<%- when StringParam -%>
memsize->memsize += yp_string_memsize(&cast-><%= param.name %>);
<%- when NodeListParam -%>
yp_node_list_memsize(&cast-><%= param.name %>, memsize);
<%- when LocationListParam -%>
memsize->memsize += yp_location_list_memsize(&cast-><%= param.name %>);
<%- when ConstantListParam -%>
memsize->memsize += yp_constant_id_list_memsize(&cast-><%= param.name %>);
<%- when YARP::StringField -%>
memsize->memsize += yp_string_memsize(&cast-><%= field.name %>);
<%- when YARP::NodeListField -%>
yp_node_list_memsize(&cast-><%= field.name %>, memsize);
<%- when YARP::LocationListField -%>
memsize->memsize += yp_location_list_memsize(&cast-><%= field.name %>);
<%- when YARP::ConstantListField -%>
memsize->memsize += yp_constant_id_list_memsize(&cast-><%= field.name %>);
<%- else -%>
<%- raise -%>
<%- end -%>

View File

@ -23,64 +23,64 @@ prettyprint_node(yp_buffer_t *buffer, yp_parser_t *parser, yp_node_t *node) {
<%- nodes.each do |node| -%>
case <%= node.type %>: {
yp_buffer_append_str(buffer, "<%= node.name %>(", <%= node.name.length + 1 %>);
<%- node.params.each_with_index do |param, index| -%>
<%- node.fields.each_with_index do |field, index| -%>
<%= "yp_buffer_append_str(buffer, \", \", 2);" if index != 0 -%>
<%- case param -%>
<%- when NodeParam -%>
prettyprint_node(buffer, parser, (yp_node_t *)((yp_<%= node.human %>_t *)node)-><%= param.name %>);
<%- when OptionalNodeParam -%>
if (((yp_<%= node.human %>_t *)node)-><%= param.name %> == NULL) {
<%- case field -%>
<%- when YARP::NodeField -%>
prettyprint_node(buffer, parser, (yp_node_t *)((yp_<%= node.human %>_t *)node)-><%= field.name %>);
<%- when YARP::OptionalNodeField -%>
if (((yp_<%= node.human %>_t *)node)-><%= field.name %> == NULL) {
yp_buffer_append_str(buffer, "nil", 3);
} else {
prettyprint_node(buffer, parser, (yp_node_t *)((yp_<%= node.human %>_t *)node)-><%= param.name %>);
prettyprint_node(buffer, parser, (yp_node_t *)((yp_<%= node.human %>_t *)node)-><%= field.name %>);
}
<%- when StringParam -%>
<%- when YARP::StringField -%>
yp_buffer_append_str(buffer, "\"", 1);
yp_buffer_append_bytes(buffer, yp_string_source(&((yp_<%= node.human %>_t *)node)-><%= param.name %>), yp_string_length(&((yp_<%= node.human %>_t *)node)-><%= param.name %>));
yp_buffer_append_bytes(buffer, yp_string_source(&((yp_<%= node.human %>_t *)node)-><%= field.name %>), yp_string_length(&((yp_<%= node.human %>_t *)node)-><%= field.name %>));
yp_buffer_append_str(buffer, "\"", 1);
<%- when NodeListParam -%>
<%- when YARP::NodeListField -%>
yp_buffer_append_str(buffer, "[", 1);
for (uint32_t index = 0; index < ((yp_<%= node.human %>_t *)node)-><%= param.name %>.size; index++) {
for (uint32_t index = 0; index < ((yp_<%= node.human %>_t *)node)-><%= field.name %>.size; index++) {
if (index != 0) yp_buffer_append_str(buffer, ", ", 2);
prettyprint_node(buffer, parser, (yp_node_t *) ((yp_<%= node.human %>_t *) node)-><%= param.name %>.nodes[index]);
prettyprint_node(buffer, parser, (yp_node_t *) ((yp_<%= node.human %>_t *) node)-><%= field.name %>.nodes[index]);
}
yp_buffer_append_str(buffer, "]", 1);
<%- when LocationListParam -%>
<%- when YARP::LocationListField -%>
yp_buffer_append_str(buffer, "[", 1);
for (uint32_t index = 0; index < ((yp_<%= node.human %>_t *)node)-><%= param.name %>.size; index++) {
for (uint32_t index = 0; index < ((yp_<%= node.human %>_t *)node)-><%= field.name %>.size; index++) {
if (index != 0) yp_buffer_append_str(buffer, ", ", 2);
prettyprint_location(buffer, parser, &((yp_<%= node.human %>_t *)node)-><%= param.name %>.locations[index]);
prettyprint_location(buffer, parser, &((yp_<%= node.human %>_t *)node)-><%= field.name %>.locations[index]);
}
yp_buffer_append_str(buffer, "]", 1);
<%- when ConstantParam -%>
char <%= param.name %>_buffer[12];
snprintf(<%= param.name %>_buffer, sizeof(<%= param.name %>_buffer), "%u", ((yp_<%= node.human %>_t *)node)-><%= param.name %>);
yp_buffer_append_str(buffer, <%= param.name %>_buffer, strlen(<%= param.name %>_buffer));
<%- when ConstantListParam -%>
<%- when YARP::ConstantField -%>
char <%= field.name %>_buffer[12];
snprintf(<%= field.name %>_buffer, sizeof(<%= field.name %>_buffer), "%u", ((yp_<%= node.human %>_t *)node)-><%= field.name %>);
yp_buffer_append_str(buffer, <%= field.name %>_buffer, strlen(<%= field.name %>_buffer));
<%- when YARP::ConstantListField -%>
yp_buffer_append_str(buffer, "[", 1);
for (uint32_t index = 0; index < ((yp_<%= node.human %>_t *)node)-><%= param.name %>.size; index++) {
for (uint32_t index = 0; index < ((yp_<%= node.human %>_t *)node)-><%= field.name %>.size; index++) {
if (index != 0) yp_buffer_append_str(buffer, ", ", 2);
char <%= param.name %>_buffer[12];
snprintf(<%= param.name %>_buffer, sizeof(<%= param.name %>_buffer), "%u", ((yp_<%= node.human %>_t *)node)-><%= param.name %>.ids[index]);
yp_buffer_append_str(buffer, <%= param.name %>_buffer, strlen(<%= param.name %>_buffer));
char <%= field.name %>_buffer[12];
snprintf(<%= field.name %>_buffer, sizeof(<%= field.name %>_buffer), "%u", ((yp_<%= node.human %>_t *)node)-><%= field.name %>.ids[index]);
yp_buffer_append_str(buffer, <%= field.name %>_buffer, strlen(<%= field.name %>_buffer));
}
yp_buffer_append_str(buffer, "]", 1);
<%- when LocationParam -%>
prettyprint_location(buffer, parser, &((yp_<%= node.human %>_t *)node)-><%= param.name %>);
<%- when OptionalLocationParam -%>
if (((yp_<%= node.human %>_t *)node)-><%= param.name %>.start == NULL) {
<%- when YARP::LocationField -%>
prettyprint_location(buffer, parser, &((yp_<%= node.human %>_t *)node)-><%= field.name %>);
<%- when YARP::OptionalLocationField -%>
if (((yp_<%= node.human %>_t *)node)-><%= field.name %>.start == NULL) {
yp_buffer_append_str(buffer, "nil", 3);
} else {
prettyprint_location(buffer, parser, &((yp_<%= node.human %>_t *)node)-><%= param.name %>);
prettyprint_location(buffer, parser, &((yp_<%= node.human %>_t *)node)-><%= field.name %>);
}
<%- when UInt32Param -%>
char <%= param.name %>_buffer[12];
snprintf(<%= param.name %>_buffer, sizeof(<%= param.name %>_buffer), "+%d", ((yp_<%= node.human %>_t *)node)-><%= param.name %>);
yp_buffer_append_str(buffer, <%= param.name %>_buffer, strlen(<%= param.name %>_buffer));
<%- when FlagsParam -%>
char <%= param.name %>_buffer[12];
snprintf(<%= param.name %>_buffer, sizeof(<%= param.name %>_buffer), "+%d", node->flags >> <%= COMMON_FLAGS %>);
yp_buffer_append_str(buffer, <%= param.name %>_buffer, strlen(<%= param.name %>_buffer));
<%- when YARP::UInt32Field -%>
char <%= field.name %>_buffer[12];
snprintf(<%= field.name %>_buffer, sizeof(<%= field.name %>_buffer), "+%d", ((yp_<%= node.human %>_t *)node)-><%= field.name %>);
yp_buffer_append_str(buffer, <%= field.name %>_buffer, strlen(<%= field.name %>_buffer));
<%- when YARP::FlagsField -%>
char <%= field.name %>_buffer[12];
snprintf(<%= field.name %>_buffer, sizeof(<%= field.name %>_buffer), "+%d", node->flags >> <%= YARP::COMMON_FLAGS %>);
yp_buffer_append_str(buffer, <%= field.name %>_buffer, strlen(<%= field.name %>_buffer));
<%- else -%>
<%- raise -%>
<%- end -%>

View File

@ -68,51 +68,51 @@ yp_serialize_node(yp_parser_t *parser, yp_node_t *node, yp_buffer_t *buffer) {
size_t length_offset = buffer->length;
yp_buffer_append_str(buffer, "\0\0\0\0", 4); /* consume 4 bytes, updated below */
<%- end -%>
<%- node.params.each do |param| -%>
<%- case param -%>
<%- when NodeParam -%>
yp_serialize_node(parser, (yp_node_t *)((yp_<%= node.human %>_t *)node)-><%= param.name %>, buffer);
<%- when OptionalNodeParam -%>
if (((yp_<%= node.human %>_t *)node)-><%= param.name %> == NULL) {
<%- node.fields.each do |field| -%>
<%- case field -%>
<%- when YARP::NodeField -%>
yp_serialize_node(parser, (yp_node_t *)((yp_<%= node.human %>_t *)node)-><%= field.name %>, buffer);
<%- when YARP::OptionalNodeField -%>
if (((yp_<%= node.human %>_t *)node)-><%= field.name %> == NULL) {
yp_buffer_append_u8(buffer, 0);
} else {
yp_serialize_node(parser, (yp_node_t *)((yp_<%= node.human %>_t *)node)-><%= param.name %>, buffer);
yp_serialize_node(parser, (yp_node_t *)((yp_<%= node.human %>_t *)node)-><%= field.name %>, buffer);
}
<%- when StringParam -%>
yp_serialize_string(parser, &((yp_<%= node.human %>_t *)node)-><%= param.name %>, buffer);
<%- when NodeListParam -%>
uint32_t <%= param.name %>_size = yp_sizet_to_u32(((yp_<%= node.human %>_t *)node)-><%= param.name %>.size);
yp_buffer_append_u32(buffer, <%= param.name %>_size);
for (uint32_t index = 0; index < <%= param.name %>_size; index++) {
yp_serialize_node(parser, (yp_node_t *) ((yp_<%= node.human %>_t *)node)-><%= param.name %>.nodes[index], buffer);
<%- when YARP::StringField -%>
yp_serialize_string(parser, &((yp_<%= node.human %>_t *)node)-><%= field.name %>, buffer);
<%- when YARP::NodeListField -%>
uint32_t <%= field.name %>_size = yp_sizet_to_u32(((yp_<%= node.human %>_t *)node)-><%= field.name %>.size);
yp_buffer_append_u32(buffer, <%= field.name %>_size);
for (uint32_t index = 0; index < <%= field.name %>_size; index++) {
yp_serialize_node(parser, (yp_node_t *) ((yp_<%= node.human %>_t *)node)-><%= field.name %>.nodes[index], buffer);
}
<%- when LocationListParam -%>
uint32_t <%= param.name %>_size = yp_sizet_to_u32(((yp_<%= node.human %>_t *)node)-><%= param.name %>.size);
yp_buffer_append_u32(buffer, <%= param.name %>_size);
for (uint32_t index = 0; index < <%= param.name %>_size; index++) {
yp_serialize_location(parser, &((yp_<%= node.human %>_t *)node)-><%= param.name %>.locations[index], buffer);
<%- when YARP::LocationListField -%>
uint32_t <%= field.name %>_size = yp_sizet_to_u32(((yp_<%= node.human %>_t *)node)-><%= field.name %>.size);
yp_buffer_append_u32(buffer, <%= field.name %>_size);
for (uint32_t index = 0; index < <%= field.name %>_size; index++) {
yp_serialize_location(parser, &((yp_<%= node.human %>_t *)node)-><%= field.name %>.locations[index], buffer);
}
<%- when ConstantParam -%>
yp_buffer_append_u32(buffer, yp_sizet_to_u32(((yp_<%= node.human %>_t *)node)-><%= param.name %>));
<%- when ConstantListParam -%>
uint32_t <%= param.name %>_size = yp_sizet_to_u32(((yp_<%= node.human %>_t *)node)-><%= param.name %>.size);
yp_buffer_append_u32(buffer, <%= param.name %>_size);
for (uint32_t index = 0; index < <%= param.name %>_size; index++) {
yp_buffer_append_u32(buffer, yp_sizet_to_u32(((yp_<%= node.human %>_t *)node)-><%= param.name %>.ids[index]));
<%- when YARP::ConstantField -%>
yp_buffer_append_u32(buffer, yp_sizet_to_u32(((yp_<%= node.human %>_t *)node)-><%= field.name %>));
<%- when YARP::ConstantListField -%>
uint32_t <%= field.name %>_size = yp_sizet_to_u32(((yp_<%= node.human %>_t *)node)-><%= field.name %>.size);
yp_buffer_append_u32(buffer, <%= field.name %>_size);
for (uint32_t index = 0; index < <%= field.name %>_size; index++) {
yp_buffer_append_u32(buffer, yp_sizet_to_u32(((yp_<%= node.human %>_t *)node)-><%= field.name %>.ids[index]));
}
<%- when LocationParam -%>
yp_serialize_location(parser, &((yp_<%= node.human %>_t *)node)-><%= param.name %>, buffer);
<%- when OptionalLocationParam -%>
if (((yp_<%= node.human %>_t *)node)-><%= param.name %>.start == NULL) {
<%- when YARP::LocationField -%>
yp_serialize_location(parser, &((yp_<%= node.human %>_t *)node)-><%= field.name %>, buffer);
<%- when YARP::OptionalLocationField -%>
if (((yp_<%= node.human %>_t *)node)-><%= field.name %>.start == NULL) {
yp_buffer_append_u8(buffer, 0);
} else {
yp_buffer_append_u8(buffer, 1);
yp_serialize_location(parser, &((yp_<%= node.human %>_t *)node)-><%= param.name %>, buffer);
yp_serialize_location(parser, &((yp_<%= node.human %>_t *)node)-><%= field.name %>, buffer);
}
<%- when UInt32Param -%>
yp_buffer_append_u32(buffer, ((yp_<%= node.human %>_t *)node)-><%= param.name %>);
<%- when FlagsParam -%>
yp_buffer_append_u32(buffer, node->flags >> <%= COMMON_FLAGS %>);
<%- when YARP::UInt32Field -%>
yp_buffer_append_u32(buffer, ((yp_<%= node.human %>_t *)node)-><%= field.name %>);
<%- when YARP::FlagsField -%>
yp_buffer_append_u32(buffer, node->flags >> <%= YARP::COMMON_FLAGS %>);
<%- else -%>
<%- raise -%>
<%- end -%>

View File

@ -4,257 +4,260 @@ require "erb"
require "fileutils"
require "yaml"
COMMON_FLAGS = 1
class Param
attr_reader :name, :options
def initialize(name:, type:, **options)
@name, @type, @options = name, type, options
end
end
module KindTypes
def c_type
if options[:kind]
"yp_#{options[:kind].gsub(/(?<=.)[A-Z]/, "_\\0").downcase}"
else
"yp_node"
end
end
def ruby_type
options[:kind] || "Node"
end
def java_type
options[:kind] || "Node"
end
def java_cast
if options[:kind]
"(Nodes.#{options[:kind]}) "
else
""
end
end
end
# This represents a parameter to a node that is itself a node. We pass them as
# references and store them as references.
class NodeParam < Param
include KindTypes
def rbs_class
ruby_type
end
end
# This represents a parameter to a node that is itself a node and can be
# optionally null. We pass them as references and store them as references.
class OptionalNodeParam < Param
include KindTypes
def rbs_class
"#{ruby_type}?"
end
end
SingleNodeParam = -> (node) { NodeParam === node or OptionalNodeParam === node }
# This represents a parameter to a node that is a list of nodes. We pass them as
# references and store them as references.
class NodeListParam < Param
def rbs_class
"Array[Node]"
end
def java_type
"Node[]"
end
end
# This represents a parameter to a node that is a list of locations.
class LocationListParam < Param
def rbs_class
"Array[Location]"
end
def java_type
"Location[]"
end
end
# This represents a parameter to a node that is the ID of a string interned
# through the parser's constant pool.
class ConstantParam < Param
def rbs_class
"Symbol"
end
def java_type
"byte[]"
end
end
# This represents a parameter to a node that is a list of IDs that are
# associated with strings interned through the parser's constant pool.
class ConstantListParam < Param
def rbs_class
"Array[Symbol]"
end
def java_type
"byte[][]"
end
end
# This represents a parameter to a node that is a string.
class StringParam < Param
def rbs_class
"String"
end
def java_type
"byte[]"
end
end
# This represents a parameter to a node that is a location.
class LocationParam < Param
def rbs_class
"Location"
end
def java_type
"Location"
end
end
# This represents a parameter to a node that is a location that is optional.
class OptionalLocationParam < Param
def rbs_class
"Location?"
end
def java_type
"Location"
end
end
# This represents an integer parameter.
class UInt32Param < Param
def rbs_class
"Integer"
end
def java_type
"int"
end
end
# This represents a set of flags. It is very similar to the UInt32Param, but can
# be directly embedded into the flags field on the struct and provides
# convenient methods for checking if a flag is set.
class FlagsParam < Param
def rbs_class
"Integer"
end
def java_type
"short"
end
def kind
options.fetch(:kind)
end
end
PARAM_TYPES = {
"node" => NodeParam,
"node?" => OptionalNodeParam,
"node[]" => NodeListParam,
"string" => StringParam,
"location[]" => LocationListParam,
"constant" => ConstantParam,
"constant[]" => ConstantListParam,
"location" => LocationParam,
"location?" => OptionalLocationParam,
"uint32" => UInt32Param,
"flags" => FlagsParam
}
# This class represents a node in the tree, configured by the config.yml file in
# YAML format. It contains information about the name of the node and the
# various child nodes it contains.
class NodeType
attr_reader :name, :type, :human, :params, :newline, :comment
def initialize(config)
@name = config.fetch("name")
type = @name.gsub(/(?<=.)[A-Z]/, "_\\0")
@type = "YP_NODE_#{type.upcase}"
@human = type.downcase
@params = config.fetch("child_nodes", []).map do |param|
param_type = PARAM_TYPES[param.fetch("type")] ||
raise("Unknown param type: #{param["type"].inspect}")
param_type.new(**param.transform_keys(&:to_sym))
end
@newline = config.fetch("newline", true)
@comment = config.fetch("comment")
end
# Should emit serialized length of node so implementations can skip
# the node to enable lazy parsing.
def needs_serialized_length?
@name == "DefNode"
end
end
# This represents a token in the lexer. They are configured through the
# config.yml file for now, but this will probably change as we transition to
# storing semantic strings instead of the lexer tokens.
class Token
attr_reader :name, :value, :comment
def initialize(config)
@name = config.fetch("name")
@value = config["value"]
@comment = config.fetch("comment")
end
def declaration
output = []
output << "YP_TOKEN_#{name}"
output << " = #{value}" if value
output << ", // #{comment}"
output.join
end
end
# Represents a set of flags that should be internally represented with an enum.
class Flags
attr_reader :name, :human, :values
def initialize(config)
@name = config.fetch("name")
@human = @name.gsub(/(?<=.)[A-Z]/, "_\\0").downcase
@values = config.fetch("values").map { |flag| Flag.new(flag) }
end
end
class Flag
attr_reader :name, :camelcase, :comment
def initialize(config)
@name = config.fetch("name")
@camelcase = @name.split("_").map(&:capitalize).join
@comment = config.fetch("comment")
end
end
module YARP
COMMON_FLAGS = 1
# This represents a field on a node. It contains all of the necessary
# information to template out the code for that field.
class Field
attr_reader :name, :options
def initialize(name:, type:, **options)
@name, @type, @options = name, type, options
end
end
# Some node fields can be specialized if they point to a specific kind of
# node and not just a generic node.
class NodeKindField < Field
def c_type
if options[:kind]
"yp_#{options[:kind].gsub(/(?<=.)[A-Z]/, "_\\0").downcase}"
else
"yp_node"
end
end
def ruby_type
options[:kind] || "Node"
end
def java_type
options[:kind] || "Node"
end
def java_cast
if options[:kind]
"(Nodes.#{options[:kind]}) "
else
""
end
end
end
# This represents a field on a node that is itself a node. We pass them as
# references and store them as references.
class NodeField < NodeKindField
def rbs_class
ruby_type
end
end
# This represents a field on a node that is itself a node and can be
# optionally null. We pass them as references and store them as references.
class OptionalNodeField < NodeKindField
def rbs_class
"#{ruby_type}?"
end
end
# This represents a field on a node that is a list of nodes. We pass them as
# references and store them directly on the struct.
class NodeListField < Field
def rbs_class
"Array[Node]"
end
def java_type
"Node[]"
end
end
# This represents a field on a node that is a list of locations.
class LocationListField < Field
def rbs_class
"Array[Location]"
end
def java_type
"Location[]"
end
end
# This represents a field on a node that is the ID of a string interned
# through the parser's constant pool.
class ConstantField < Field
def rbs_class
"Symbol"
end
def java_type
"byte[]"
end
end
# This represents a field on a node that is a list of IDs that are associated
# with strings interned through the parser's constant pool.
class ConstantListField < Field
def rbs_class
"Array[Symbol]"
end
def java_type
"byte[][]"
end
end
# This represents a field on a node that is a string.
class StringField < Field
def rbs_class
"String"
end
def java_type
"byte[]"
end
end
# This represents a field on a node that is a location.
class LocationField < Field
def rbs_class
"Location"
end
def java_type
"Location"
end
end
# This represents a field on a node that is a location that is optional.
class OptionalLocationField < Field
def rbs_class
"Location?"
end
def java_type
"Location"
end
end
# This represents an integer field.
class UInt32Field < Field
def rbs_class
"Integer"
end
def java_type
"int"
end
end
# This represents a set of flags. It is very similar to the UInt32Field, but
# can be directly embedded into the flags field on the struct and provides
# convenient methods for checking if a flag is set.
class FlagsField < Field
def rbs_class
"Integer"
end
def java_type
"short"
end
def kind
options.fetch(:kind)
end
end
# This class represents a node in the tree, configured by the config.yml file in
# YAML format. It contains information about the name of the node and the
# various child nodes it contains.
class NodeType
attr_reader :name, :type, :human, :fields, :newline, :comment
def initialize(config)
@name = config.fetch("name")
type = @name.gsub(/(?<=.)[A-Z]/, "_\\0")
@type = "YP_NODE_#{type.upcase}"
@human = type.downcase
@fields =
config.fetch("fields", []).map do |field|
field_type_for(field.fetch("type")).new(**field.transform_keys(&:to_sym))
end
@newline = config.fetch("newline", true)
@comment = config.fetch("comment")
end
# Should emit serialized length of node so implementations can skip
# the node to enable lazy parsing.
def needs_serialized_length?
name == "DefNode"
end
private
def field_type_for(name)
case name
when "node" then NodeField
when "node?" then OptionalNodeField
when "node[]" then NodeListField
when "string" then StringField
when "location[]" then LocationListField
when "constant" then ConstantField
when "constant[]" then ConstantListField
when "location" then LocationField
when "location?" then OptionalLocationField
when "uint32" then UInt32Field
when "flags" then FlagsField
else raise("Unknown field type: #{name.inspect}")
end
end
end
# This represents a token in the lexer.
class Token
attr_reader :name, :value, :comment
def initialize(config)
@name = config.fetch("name")
@value = config["value"]
@comment = config.fetch("comment")
end
def declaration
output = []
output << "YP_TOKEN_#{name}"
output << " = #{value}" if value
output << ", // #{comment}"
output.join
end
end
# Represents a set of flags that should be internally represented with an enum.
class Flags
# Represents an individual flag within a set of flags.
class Flag
attr_reader :name, :camelcase, :comment
def initialize(config)
@name = config.fetch("name")
@camelcase = @name.split("_").map(&:capitalize).join
@comment = config.fetch("comment")
end
end
attr_reader :name, :human, :values
def initialize(config)
@name = config.fetch("name")
@human = @name.gsub(/(?<=.)[A-Z]/, "_\\0").downcase
@values = config.fetch("values").map { |flag| Flag.new(flag) }
end
end
class << self
# This templates out a file using ERB with the given locals. The locals are
# derived from the config.yml file.