nodes repo

Shared build scripts and metadata for lib-ruby-parser repos.

Documentation

Basic API

Nodes

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

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); ```

Templates support

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:

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:

Custom filters can be registered by calling .with_filter on a template:

```rust use liquidcore::{ Result, Runtime, Value, ValueView, Displayfilter, Filter, FilterReflection, ParseFilter, };

[derive(Clone, ParseFilter, FilterReflection)]

[filter(

name = "append_foo",
description = "Appends 'foo' to a given string.",
parsed(AppendFooFilter)

)] pub struct AppendFoo;

[derive(Debug, Default, Display_filter)]

[name = "append_foo"]

struct AppendFooFilter;

impl Filter for AppendFooFilter { fn evaluate(&self, input: &dyn ValueView, runtime: &dyn Runtime) -> Result { let input = input.tokstr(); let output = format!("{}foo", input); Ok(Value::scalar(output)) } }

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.