TraceMaker
C++ API of the engine
Loading...
Searching...
No Matches
tmk::sexpr::Document Class Reference

#include <sexpr/sexpr.hpp>

Public Member Functions

NodeId root () const
const Node & node (NodeId id) const
std::size_t node_count () const
const std::string & text () const
std::span< const NodeId > children (NodeId list) const
 -— navigation -—
NodeId child (NodeId list, std::size_t i) const
std::string_view head (NodeId list) const
 First token of a list, e.g. "segment" for (segment ...). Empty for atoms or empty lists.
bool is_list (NodeId id) const
NodeId find (NodeId list, std::string_view name) const
 First child list whose head is name, or kNoNode.
std::vector< NodeId > find_all (NodeId list, std::string_view name) const
 All child lists whose head is name.
std::string_view raw (NodeId id) const
 -— atoms -— Raw source text of a node (for strings, including quotes).
std::string str (NodeId atom) const
 Atom value: symbols as-is, strings unescaped.
std::string str_at (NodeId list, std::size_t i) const
 Value of the i-th child atom of list (i counts the head as 0).
std::optional< double > number_at (NodeId list, std::size_t i) const
std::optional< std::int64_t > nm_at (NodeId list, std::size_t i) const
 Parses a decimal millimetre value to integer nanometres exactly (no binary floating point).
int line_of (std::size_t offset) const
 1-based line number of a byte offset (for error messages).
void replace (NodeId id, std::string new_text)
 -— edits (applied by write()) -— Replaces the node's text.
void remove (NodeId id)
 Removes the node together with its leading whitespace on the same line and the line break, when the node occupies whole lines; otherwise removes the node and one preceding space.
void append_child (NodeId list, std::string_view text)
 Inserts text as new children at the end of list, each on its own line, indented one level deeper than the list's own line.
bool modified () const
std::string write () const
 Text after applying all edits.
void save (const std::string &path) const
std::string_view indent_of (std::size_t offset) const
 The indentation (tabs/spaces) at the start of the line containing offset.

Static Public Member Functions

static Document parse (std::string text)
 Parses text. Throws ParseError on malformed input. The document owns a copy of the text.
static Document load (const std::string &path)

Detailed Description

Definition at line 45 of file sexpr.hpp.

Member Function Documentation

◆ append_child()

void tmk::sexpr::Document::append_child ( NodeId list,
std::string_view text )

Inserts text as new children at the end of list, each on its own line, indented one level deeper than the list's own line.

text must be one or more complete s-expressions without leading indent; embedded newlines are re-indented.

◆ child()

NodeId tmk::sexpr::Document::child ( NodeId list,
std::size_t i ) const
inline

Definition at line 61 of file sexpr.hpp.

◆ children()

std::span< const NodeId > tmk::sexpr::Document::children ( NodeId list) const
inline

-— navigation -—

Definition at line 57 of file sexpr.hpp.

◆ find()

NodeId tmk::sexpr::Document::find ( NodeId list,
std::string_view name ) const

First child list whose head is name, or kNoNode.

◆ find_all()

std::vector< NodeId > tmk::sexpr::Document::find_all ( NodeId list,
std::string_view name ) const

All child lists whose head is name.

◆ head()

std::string_view tmk::sexpr::Document::head ( NodeId list) const

First token of a list, e.g. "segment" for (segment ...). Empty for atoms or empty lists.

◆ indent_of()

std::string_view tmk::sexpr::Document::indent_of ( std::size_t offset) const

The indentation (tabs/spaces) at the start of the line containing offset.

◆ is_list()

bool tmk::sexpr::Document::is_list ( NodeId id) const
inline

Definition at line 67 of file sexpr.hpp.

◆ line_of()

int tmk::sexpr::Document::line_of ( std::size_t offset) const

1-based line number of a byte offset (for error messages).

◆ load()

Document tmk::sexpr::Document::load ( const std::string & path)
static

◆ modified()

bool tmk::sexpr::Document::modified ( ) const
inline

Definition at line 99 of file sexpr.hpp.

◆ nm_at()

std::optional< std::int64_t > tmk::sexpr::Document::nm_at ( NodeId list,
std::size_t i ) const

Parses a decimal millimetre value to integer nanometres exactly (no binary floating point).

◆ node()

const Node & tmk::sexpr::Document::node ( NodeId id) const
inline

Definition at line 52 of file sexpr.hpp.

◆ node_count()

std::size_t tmk::sexpr::Document::node_count ( ) const
inline

Definition at line 53 of file sexpr.hpp.

◆ number_at()

std::optional< double > tmk::sexpr::Document::number_at ( NodeId list,
std::size_t i ) const

◆ parse()

Document tmk::sexpr::Document::parse ( std::string text)
static

Parses text. Throws ParseError on malformed input. The document owns a copy of the text.

◆ raw()

std::string_view tmk::sexpr::Document::raw ( NodeId id) const
inline

-— atoms -— Raw source text of a node (for strings, including quotes).

Definition at line 75 of file sexpr.hpp.

◆ remove()

void tmk::sexpr::Document::remove ( NodeId id)

Removes the node together with its leading whitespace on the same line and the line break, when the node occupies whole lines; otherwise removes the node and one preceding space.

◆ replace()

void tmk::sexpr::Document::replace ( NodeId id,
std::string new_text )

-— edits (applied by write()) -— Replaces the node's text.

◆ root()

NodeId tmk::sexpr::Document::root ( ) const
inline

Definition at line 51 of file sexpr.hpp.

◆ save()

void tmk::sexpr::Document::save ( const std::string & path) const

◆ str()

std::string tmk::sexpr::Document::str ( NodeId atom) const

Atom value: symbols as-is, strings unescaped.

◆ str_at()

std::string tmk::sexpr::Document::str_at ( NodeId list,
std::size_t i ) const

Value of the i-th child atom of list (i counts the head as 0).

◆ text()

const std::string & tmk::sexpr::Document::text ( ) const
inline

Definition at line 54 of file sexpr.hpp.

◆ write()

std::string tmk::sexpr::Document::write ( ) const

Text after applying all edits.


The documentation for this class was generated from the following file: