|
| 1 | +module YARP |
| 2 | + # The DSL module provides a set of methods that can be used to create YARP |
| 3 | + # nodes in a more concise manner. For example, instead of writing: |
| 4 | + # |
| 5 | + # source = YARP::Source.new("[1]") |
| 6 | + # |
| 7 | + # YARP::ArrayNode.new( |
| 8 | + # [ |
| 9 | + # YARP::IntegerNode.new( |
| 10 | + # YARP::IntegerBaseFlags::DECIMAL, |
| 11 | + # YARP::Location.new(source, 1, 1), |
| 12 | + # ) |
| 13 | + # ], |
| 14 | + # YARP::Location.new(source, 0, 1), |
| 15 | + # YARP::Location.new(source, 2, 1) |
| 16 | + # ) |
| 17 | + # |
| 18 | + # you could instead write: |
| 19 | + # |
| 20 | + # source = YARP::Source.new("[1]") |
| 21 | + # |
| 22 | + # ArrayNode( |
| 23 | + # IntegerNode(YARP::IntegerBaseFlags::DECIMAL, Location(source, 1, 1))), |
| 24 | + # Location(source, 0, 1), |
| 25 | + # Location(source, 2, 1) |
| 26 | + # ) |
| 27 | + # |
| 28 | + # This is mostly helpful in the context of writing tests, but can also be used |
| 29 | + # to generate trees programmatically. |
| 30 | + module DSL |
| 31 | + private |
| 32 | + |
| 33 | + # Create a new Location object |
| 34 | + def Location(source = nil, start_offset = 0, length = 0) |
| 35 | + Location.new(source, start_offset, length) |
| 36 | + end |
| 37 | + <%- nodes.each do |node| -%> |
| 38 | + |
| 39 | + # Create a new <%= node.name %> node |
| 40 | + def <%= node.name %>(<%= (node.fields.map(&:name) + ["location = Location()"]).join(", ") %>) |
| 41 | + <%= node.name %>.new(<%= (node.fields.map(&:name) + ["location"]).join(", ") %>) |
| 42 | + end |
| 43 | + <%- end -%> |
| 44 | + end |
| 45 | +end |
0 commit comments