Shared build scripts and metadata for lib-ruby-parser
repos.
AST Nodes information is represented by Node
/NodeField
/NodeFieldType
types, you can retrieve all nodes
by calling nodes()
function. Every known Node has name and a list of fields (and both have comments as data):
```rust use librubyparser_nodes::{nodes, Node, NodeField, NodeFieldType};
let allnodes = nodes(); asserteq!(nodes().len(), 124);
let defnode: &Node = allnodes .iter() .find(|node| node.camelcasename == "Def") .unwrap(); asserteq!(def_node.fields.len(), 8);
let expressionlfield: &NodeField = defnode .fields .iter() .find(|field| field.snakecasename == "expressionl") .unwrap(); asserteq!(expressionlfield.field_type, NodeFieldType::Loc); ```
Messages have the same API, but instead Message
/MessageField
/MessageFieldType
types are used:
```rust use librubyparser_nodes::{messages, Message, MessageField, MessageFieldType};
let allmessages = messages(); asserteq!(messages().len(), 90);
let nthrefistoobigmessage: &Message = allmessages .iter().find(|message| message.camelcasename == "NthRefIsTooBig").unwrap(); asserteq!(nthrefistoobig_message.fields.len(), 1);
let nthreffield: &MessageField = nthrefistoobigmessage .fields .iter() .find(|field| field.snakecasename == "nthref") .unwrap(); asserteq!(nthreffield.field_type, MessageFieldType::Str); ```
This repo is use mostly for code generation in other repos. There are more than 100 nodes and 100 messages, and so code generation is the best way to handle them.
Liquid is used as a primary template language:
```rust use librubyparser_nodes::LiquidTemplate;
let template = LiquidTemplate::new_eval(" Nodes count: {{ nodes.size }} Messages count: {{ messages.size }} "); // Or LiquidTemplate::new("path/to/file.liquid")
assert_eq!( template.render().trim(), "Nodes count: 124\nMessages count: 90" ); ```
By default the following globals are available:
nodes
- set to lib_ruby_parser_nodes::nodes()
messages
- set to lib_ruby_parser_nodes::messages()
It's possible to register additional data by calling .with_global
on a template:
```rust use librubyparser_nodes::{ LiquidTemplate, reexports::liquid::value, };
let output = LiquidTemplate::neweval("{{ customglobal }} bar") .withglobal("customglobal", value!("foo")) .render();
assert_eq!(output, "foo bar") ```
By default the following filters are available:
| camelcase_to_snakecase
- converts FooBar
string to foo_bar
| snakecase_to_camelcase
- converts foo_bar
string to FooBar
| escape_c_keyword
- appends _
to a string if it's a C keyword| escape_cpp_keyword
- appends _
to a string if it's a C++ keyword| escape_rust_keyword
- appends _
to a string if it's a Rust keyword| render_comment: "//", 4
- renders array of strings (like node.comment
) to a string where each line is prefixed with "//" and has 4 spaces padding (except for the first line, it has no padding)Custom filters can be registered by calling .with_filter
on a template:
```rust use liquidcore::{ Result, Runtime, Value, ValueView, Displayfilter, Filter, FilterReflection, ParseFilter, };
name = "append_foo",
description = "Appends 'foo' to a given string.",
parsed(AppendFooFilter)
)] pub struct AppendFoo;
struct AppendFooFilter;
impl Filter for AppendFooFilter {
fn evaluate(&self, input: &dyn ValueView, runtime: &dyn Runtime) -> Result
use librubyparser_nodes::LiquidTemplate;
let template = LiquidTemplate::neweval("{{ 'data ' | appendfoo }}") .with_filter(AppendFoo) .render();
assert_eq!(template, "data foo"); ```
You can also check more complicated filter (like render_comment
) to understand how parameters can be passed.