NAME
lang/lisp/parser - Parse Lisp/Scheme S-expressions.
SYNOPSIS
from lang/lisp/parser import parse_program, parse_sexpr, sym;
let expr := parse_sexpr("(+ 1 2)");
say( expr[0].get_name() ); // +
let direct := [ sym("+"), 1, 2 ];
let program := parse_program("(define x 1)\n(+ x 2)", "example.lizp");
say( program.location_for( program.get_exprs()[1] ) ); // example.lizp:2:1
DESCRIPTION
This module provides a general-purpose S-expression reader for Lisp/Scheme-style source text. It only parses; evaluation is provided by lang/lisp/eval.
Parsed values use ordinary Zuzu values where that is unambiguous:
- proper lists become
Arrayvalues; - strings become
Stringvalues; - numbers become
Numbervalues; #tand#fbecomeBooleanvalues;- symbols become
LispSymbolobjects; - dotted pairs become
LispPairchains. - vector literals become
LispVectorobjects. - character literals become
LispCharobjects. - bytevector literals become
LispByteVectorobjects.
Symbols are objects so callers can pass nested Zuzu arrays directly to the evaluator while keeping Zuzu strings available as Lisp string literals. Use sym(name) when building those arrays in ZuzuScript.
The reader supports line comments beginning with ;, POD blocks beginning with a column-one directive such as =head1 and ending at =cut, double-quoted strings with simple escapes, quote syntax ', quasiquote syntax `, unquote syntax ,, unquote-splicing syntax ,@, dotted pairs, vector syntax #(...), character syntax #\x, and bytevector syntax #u8(...).
parse_program and load_program keep source text, source name, and a source map. This is useful for tools that want diagnostics or editor integration.
EXPORTS
Functions
parse_sexpr(String text)Parses
textand returns one S-expression value. Throws if the input contains zero expressions, more than one expression, an unterminated string or list, an unexpected closing parenthesis, or an invalid dotted pair.parse_sexprs(String text)Parses
textand returns anArraycontaining every S-expression in the input. Whitespace, line comments, and embedded POD blocks are skipped between expressions.parse_program(String text, String source_name?)Parses
textand returns aLispProgram.source_namedefaults to<string>and is used in source locations.load_sexprs(Path path)Reads UTF-8 text from a
std/ioPathand returns the same value asparse_sexprs.load_program(Path path)Reads UTF-8 text from a
std/ioPathand returns the same value asparse_program, using the path text as the source name.sym(String name)Returns the interned
LispSymbolforname. This is the preferred way to build direct nested-array S-expressions:lisp_eval( [ sym("+"), 1, 2 ], env );is_symbol(value)Returns
truewhenvalueis aLispSymbol, otherwisefalse.
Classes
LispErrorBase exception type for Lisp parser and evaluator errors. It carries an optional
location()value when a caller attaches one.LispSyntaxErrorSubclass of
LispErrorreserved for syntax-level failures.LispRuntimeErrorSubclass of
LispErrorused by the evaluator for Lisp-level runtime errors.LispSymbolRepresents a Lisp symbol.
symbol.get_name()Returns the symbol name as a
String.symbol.to_String()Returns the symbol name for string coercion.
LispPairRepresents a dotted pair.
pair.get_car()Returns the pair head.
pair.get_cdr()Returns the pair tail.
LispVectorRepresents a Lisp vector.
vector.get_values()Returns the underlying
Array.vector.get_values().length()Returns the number of values in the vector.
vector.ref(Number index)Returns the value at
index.vector.set_value(Number index, value)Sets and returns the value at
index.vector.to_Array()Returns a shallow copy of the vector values as an
Array.
LispCharRepresents a Lisp character.
char.get_value()Returns the character as a one-character
String.
LispByteVectorRepresents a bytevector.
bytevector.get_values()Returns the underlying
Arrayof byte values.bytevector.get_values().length()Returns the number of bytes.
bytevector.ref(Number index)Returns the byte at
index.bytevector.set_value(Number index, Number value)Sets and returns the byte at
index.bytevector.to_Array()Returns a shallow copy of the byte values.
LispEOFRepresents the Lisp end-of-file object used by ports.
LispSourceLocationRepresents a source position.
location.get_source_name()Returns the source name.
location.get_index()Returns the zero-based character index.
location.get_line()Returns the one-based line number.
location.get_column()Returns the one-based column number.
location.to_String()Returns
source:line:column.
LispProgramParsed source plus source metadata.
program.get_exprs()Returns the parsed top-level expressions.
program.get_source()Returns the original source text.
program.get_source_name()Returns the source name used while parsing.
program.get_source_map()Returns source-map entries. Each entry contains
valueandlocation.program.location_for(value)Returns the first recorded
LispSourceLocationforvalue, ornullif no location is recorded.
SExprReaderStateful reader used by the convenience functions. Most callers should use
parse_sexpr,parse_sexprs, orparse_program.reader.get_source_map()Returns source-map entries recorded by this reader.
reader.read_expr()Reads and returns one expression from the current reader position.
reader.read_all()Reads all remaining expressions and returns them as an
Array.reader.eof()Skips whitespace and comments, then returns whether the reader is at end of input.
COPYRIGHT AND LICENCE
lang/lisp/parser is copyright Toby Inkster.
It is free software; you may redistribute it and/or modify it under the terms of either the Artistic License 1.0 or the GNU General Public License version 2.