Ergo.Context (ergo v0.1.0)

Link to this section Summary

Functions

Because we build ASTs using lists they end up in reverse order. This method reverses the AST back to in-parse-order

Where an AST has been built from individual characters and needs to be converted to a string

Called to perform an arbitrary transformation on the AST value of a Context.

The ignore parser matches but returns a nil for the AST. Parsers like sequence accumulate these nil values. Call this function to remove them

Examples

iex> Context.new() %Context{}

Examples:

iex> Context.new("Hello World") %Context{status: :ok, input: "Hello World", line: 1, col: 1, index: 0}

Examples

iex> Context.next_char(Context.new()) %Context{status: {:error, :unexpected_eoi}, message: "Unexpected end of input"}

Examples

iex> context = Context.new("Hello")
...> Context.peek(context)
%Context{status: :ok, char: ?H, ast: ?H, input: "ello", index: 1, line: 1, col: 2}

iex> context = Context.new()
...> Context.peek(context)
%Context{status: {:error, :unexpected_eoi}, message: "Unexpected end of input", index: 0, line: 1, col: 1}

Link to this section Functions

Link to this function

ast_in_parsed_order(ctx)

Because we build ASTs using lists they end up in reverse order. This method reverses the AST back to in-parse-order

Examples

iex> context = Ergo.Context.new()
...> context = %{context | ast: [4, 3, 2, 1]}
...> Context.ast_in_parsed_order(context)
%Context{ast: [1, 2, 3, 4]}
Link to this function

ast_to_string(ctx)

Where an AST has been built from individual characters and needs to be converted to a string

Examples

iex> context = Ergo.Context.new()
...> context = %{context | ast: [?H, ?e, ?l, ?l, ?o]}
...> Context.ast_to_string(context)
%Context{ast: "Hello"}
Link to this function

ast_transform(ctx, fun)

Called to perform an arbitrary transformation on the AST value of a Context.

Examples

iex> alias Ergo.Context
...> context = Context.new()
...> context = %{context | ast: "Hello World"}
...> Context.ast_transform(context, &Function.identity/1)
%Context{ast: "Hello World"}

iex> alias Ergo.Context
...> context = Context.new()
...> context = %{context | ast: "Hello World"}
...> Context.ast_transform(context, &String.length/1)
%Context{ast: 11}
Link to this function

ast_without_ignored(ctx)

The ignore parser matches but returns a nil for the AST. Parsers like sequence accumulate these nil values. Call this function to remove them

Examples

iex> context = Ergo.Context.new()
...> context = %{context | ast: ["Hello", nil, "World", nil]}
...> Context.ast_without_ignored(context)
%Context{ast: ["Hello", "World"]}

Examples

iex> Context.new() %Context{}

Examples:

iex> Context.new("Hello World") %Context{status: :ok, input: "Hello World", line: 1, col: 1, index: 0}

Link to this function

next_char(context)

Examples

iex> Context.next_char(Context.new()) %Context{status: {:error, :unexpected_eoi}, message: "Unexpected end of input"}

iex> Context.next_char(Context.new("Hello World")) %Context{status: :ok, input: "ello World", char: ?H, ast: ?H, index: 1, line: 1, col: 2}

Examples

iex> context = Context.new("Hello")
...> Context.peek(context)
%Context{status: :ok, char: ?H, ast: ?H, input: "ello", index: 1, line: 1, col: 2}

iex> context = Context.new()
...> Context.peek(context)
%Context{status: {:error, :unexpected_eoi}, message: "Unexpected end of input", index: 0, line: 1, col: 1}