Ergo.Terminals (ergo v0.3.5)
Ergo.Terminals
contains the terminal parsers.
A terminal parser is a parser that is not parameterised with another parser and works directly with the input.
Parsers
- eoi
- char
- digit
- alpha
- ws
- wc
- literal
Link to this section Summary
Link to this section Functions
alpha()
The alpha/0
parser accepts a single character in the range a..z or A..Z.
Examples
iex> alias Ergo.Context
iex> import Ergo.Terminals
iex> parser = alpha()
iex> Ergo.parse(parser, "Hello World")
%Context{status: :ok, input: "ello World", char: ?H, ast: ?H, index: 1, line: 1, col: 2}
iex> alias Ergo.Context
iex> import Ergo.Terminals
iex> parser = alpha()
iex> Ergo.parse(parser, "ello World")
%Context{status: :ok, input: "llo World", char: ?e, ast: ?e, index: 1, line: 1, col: 2}
iex> alias Ergo.Context
iex> import Ergo.Terminals
iex> parser = alpha()
iex> Ergo.parse(parser, " World")
%Context{status: {:error, :unexpected_char}, message: "Expected: [a..z, A..Z] Actual: ", input: " World"}
char(c)
The char/1
parser is a terminal parser that matches a specific character.
Examples
iex> alias Ergo.Context
iex> import Ergo.Terminals
iex> parser = char(?H)
iex> Ergo.parse(parser, "Hello World")
%Context{status: :ok, char: ?H, ast: ?H, input: "ello World", index: 1, line: 1, col: 2}
iex> alias Ergo.Context
iex> import Ergo.Terminals
iex> parser = char(?h)
iex> Ergo.parse(parser, "Hello World")
%Context{status: {:error, :unexpected_char}, message: "Expected: h Actual: H", input: "Hello World"}
iex> alias Ergo.Context
iex> import Ergo.Terminals
iex> parser = char(?H)
iex> Ergo.parse(parser, "")
%Context{status: {:error, :unexpected_eoi}, message: "Unexpected end of input"}
iex> alias Ergo.Context
iex> import Ergo.Terminals
iex> parser = char(?A..?Z)
iex> Ergo.parse(parser, "Hello World")
%Context{status: :ok, char: ?H, ast: ?H, input: "ello World", index: 1, line: 1, col: 2}
iex> alias Ergo.Context
iex> import Ergo.Terminals
iex> parser = char(?a..?z)
iex> Ergo.parse(parser, "Hello World")
%Context{status: {:error, :unexpected_char}, message: "Expected: a..z Actual: H", input: "Hello World"}
iex> alias Ergo.Context
iex> import Ergo.Terminals
iex> parser = char(?A..?Z)
iex> Ergo.parse(parser, "")
%Context{status: {:error, :unexpected_eoi}, message: "Unexpected end of input"}
iex> alias Ergo.Context
iex> import Ergo.Terminals
iex> parser = char([?a..?z, ?A..?Z])
iex> Ergo.parse(parser, "Hello World")
%Context{status: :ok, char: ?H, ast: ?H, input: "ello World", index: 1, line: 1, col: 2}
iex> alias Ergo.Context
iex> import Ergo.Terminals
iex> parser = char([?a..?z, ?A..?Z])
iex> Ergo.parse(parser, "0000")
%Context{status: {:error, :unexpected_char}, message: "Expected: [a..z, A..Z] Actual: 0", input: "0000"}
iex> alias Ergo.Context
iex> import Ergo.Terminals
iex> parser = char(-?0)
iex> Ergo.parse(parser, "0000")
%Context{status: {:error, :unexpected_char}, message: "Should not have matched 0", input: "0000"}
iex> alias Ergo.Context
iex> import Ergo.Terminals
iex> parser = char(-?a)
iex> Ergo.parse(parser, "0000")
%Context{status: :ok, input: "000", ast: ?0, char: ?0, index: 1, col: 2}
digit()
The digit/0
parser accepts a character in the range of 0..9
Examples
iex> alias Ergo.Context
iex> import Ergo.Terminals
iex> parser = digit()
iex> Ergo.parse(parser, "0000")
%Context{status: :ok, char: ?0, ast: ?0, input: "000", index: 1, line: 1, col: 2}
iex> alias Ergo.Context
iex> import Ergo.Terminals
iex> import Ergo.Terminals
iex> parser = digit()
iex> Ergo.parse(parser, "AAAA")
%Context{status: {:error, :unexpected_char}, message: "Expected: 0..9 Actual: A", char: 0, input: "AAAA", index: 0, line: 1, col: 1}
iex> alias Ergo.{Context, Parser}
iex> import Ergo.Terminals
iex> context = Context.new()
iex> parser = digit()
iex> Parser.call(parser, context)
%Context{status: {:error, :unexpected_eoi}, message: "Unexpected end of input", char: 0, input: "", index: 0, line: 1, col: 1}
eoi()
The eoi parser is a terminal parser that checks whether the input has been fully consumed. If there is input remaining to be parsed the return context status is set to :error.
Examples
iex> alias Ergo.{Context, Parser}
iex> import Ergo.Terminals
iex> context = Context.new()
iex> Parser.call(eoi(), context)
%Context{status: :ok, ast: nil}
iex> alias Ergo.{Context, Parser}
iex> import Ergo.Terminals
iex> context = Context.new("Hello World")
iex> Parser.call(eoi(), context)
%Context{status: {:error, :not_eoi}, message: "Input not empty: Hello World…", input: "Hello World"}
literal(s, opts \\ [])
The literal/1
parser matches the specified string character by character.
Examples
iex> alias Ergo.Context
iex> import Ergo.Terminals
iex> parser = literal("Hello")
iex> Ergo.parse(parser, "Hello World")
%Context{status: :ok, input: " World", ast: "Hello", char: ?o, index: 5, line: 1, col: 6}
iex> alias Ergo.Context
iex> import Ergo.Terminals
iex> parser = literal("Hellx")
iex> Ergo.parse(parser, "Hello World")
%Context{status: {:error, :unexpected_char}, message: "Expected: x Actual: o [in literal \"Hellx\"]", input: "o World", ast: [?l, ?l, ?e, ?H], char: ?l, index: 4, line: 1, col: 5}
The wc/0
parser parses a word character and is analagous to the \w regular expression.
Examples
iex> alias Ergo.Context
iex> import Ergo.Terminals
iex> parser = wc()
iex> Ergo.parse(parser, "Hello World")
%Context{status: :ok, char: ?H, ast: ?H, input: "ello World", index: 1, col: 2}
iex> alias Ergo.Context
iex> import Ergo.Terminals
iex> parser = wc()
iex> Ergo.parse(parser, "0 World")
%Context{status: :ok, char: ?0, ast: ?0, input: " World", index: 1, col: 2}
iex> alias Ergo.Context
iex> import Ergo.Terminals
iex> parser = wc()
iex> Ergo.parse(parser, "_Hello")
%Context{status: :ok, char: ?_, ast: ?_, input: "Hello", index: 1, col: 2}
iex> alias Ergo.Context
iex> import Ergo.Terminals
iex> parser = wc()
iex> Ergo.parse(parser, " Hello")
%Context{status: {:error, :unexpected_char}, message: "Expected: [0..9, a..z, A..Z, _] Actual: ", input: " Hello"}
The ws/0
parser accepts a white space character and is equivalent to the \s regular expression.
Examples
iex> alias Ergo.Context
iex> import Ergo.Terminals
iex> parser = ws()
iex> Ergo.parse(parser, " World")
%Context{status: :ok, char: ?\s, ast: ?\s, input: "World", index: 1, line: 1, col: 2}
iex> alias Ergo.Context
iex> import Ergo.Terminals
iex> parser = ws()
iex> Ergo.parse(parser, "\tWorld")
%Context{status: :ok, char: ?\t, ast: ?\t, input: "World", index: 1, line: 1, col: 2}
iex> alias Ergo.Context
iex> import Ergo.Terminals
iex> parser = ws()
iex> Ergo.parse(parser, "\nWorld")
%Context{status: :ok, char: ?\n, ast: ?\n, input: "World", index: 1, line: 2, col: 1}
iex> alias Ergo.Context
iex> import Ergo.Terminals
iex> parser = ws()
iex> Ergo.parse(parser, "Hello World")
%Context{status: {:error, :unexpected_char}, message: "Expected: [\s, \t, \r, \n, \v] Actual: H", input: "Hello World"}