Tptp.Token (Tptp v0.1.0)

Copy Markdown View Source

The terminal vocabulary of the TPTP grammar.

This module is the single source of truth shared by three consumers:

A token is a plain three-tuple {category, offset, length} — never a struct and never carrying a binary. yecc reads elem(token, 0) as the category and elem(token, 1) as the position, so a parse error reports a byte offset directly. Token text is recovered with binary_part/3 during the CST post-pass, which keeps the scan loop free of binary allocation.

Every atom named here is created at compile time. Nothing in this library ever calls String.to_atom/1 on input; see Tptp.Checks.NoDynamicAtoms, the custom Credo check that enforces it mechanically.

Summary

Types

A terminal category. Always a compile-time atom.

t()

A lexed token: category, byte offset, byte length.

Functions

Every terminal category, operators and keywords and value categories alike.

The category for a literal spelling as it appears in the BNF, or nil.

The six $-word keywords, which are also legal <dollar_word>s.

Operators and punctuation only, longest spelling first.

Whether a category is pure punctuation, and so dropped from the CST.

The four <source> keywords, which are also legal <atomic_word>s.

The fixed text of a category, or nil for a value-carrying category.

{category, spelling} pairs for every fixed-text terminal, longest spelling first.

The seven language keywords that open a statement.

Categories whose text must be carried because the category does not determine it.

Types

category()

@type category() :: atom()

A terminal category. Always a compile-time atom.

t()

@type t() :: {category(), non_neg_integer(), non_neg_integer()}

A lexed token: category, byte offset, byte length.

Functions

categories()

@spec categories() :: [category()]

Every terminal category, operators and keywords and value categories alike.

category_for(spelling)

@spec category_for(binary()) :: category() | nil

The category for a literal spelling as it appears in the BNF, or nil.

iex> Tptp.Token.category_for("~|")
:nor
iex> Tptp.Token.category_for("wibble")
nil

dollar_keywords()

@spec dollar_keywords() :: [{category(), binary()}]

The six $-word keywords, which are also legal <dollar_word>s.

operators()

@spec operators() :: [{category(), binary()}]

Operators and punctuation only, longest spelling first.

This is what Tptp.Lexer generates its scan clauses from. Keywords are excluded deliberately: a keyword must be recognised by scanning the whole word and then looking it up, never by matching a prefix, or filename lexes as file followed by name.

punctuation?(category)

@spec punctuation?(category()) :: boolean()

Whether a category is pure punctuation, and so dropped from the CST.

iex> Tptp.Token.punctuation?(:comma)
true
iex> Tptp.Token.punctuation?(:ampersand)
false

source_keywords()

@spec source_keywords() :: [{category(), binary()}]

The four <source> keywords, which are also legal <atomic_word>s.

spelling(category)

@spec spelling(category()) :: binary() | nil

The fixed text of a category, or nil for a value-carrying category.

iex> Tptp.Token.spelling(:iff)
"<=>"
iex> Tptp.Token.spelling(:lower_word)
nil

spellings()

@spec spellings() :: [{category(), binary()}]

{category, spelling} pairs for every fixed-text terminal, longest spelling first.

Sorted so that a caller matching prefixes in list order gets maximal munch for free.

statement_keywords()

@spec statement_keywords() :: [{category(), binary()}]

The seven language keywords that open a statement.

value_categories()

@spec value_categories() :: [category()]

Categories whose text must be carried because the category does not determine it.