scheme

Scheme.

Tries to adhere to the official Scheme syntax, especially the complicated number syntax. See for more information:

Besides the Scheme language definition and a subclass SchemeLily that is used for Scheme code embedded into LilyPond, there is a helper function scheme_number() that parses the Scheme number tokens into a Python value. This function can be used when transforming/parsing the Scheme tokens into some data model that access the Scheme values.

In this module:

Language

Name (Aliases)

Description

Filename(s)

Mime Type(s)

Scheme

Scheme (guile)

Scheme programming language

*.scm

text/x-script.scheme, text/x-script.guile

SchemeLily

(not registered)

class Scheme[source]

Bases: parce.language.Language

root
classmethod common(pop=0)[source]

Yield common stuff. pop can be set to -1 for one-arg mode.

list
vector
classmethod get_word_action()[source]

Return a dynamic action that is chosen based on the text.

number

Decimal numbers, derive with 2 for binary, 8 for octal, 16 for hexadecimal numbers.

string
classmethod string_common()[source]
multiline_comment
singleline_comment
class SchemeLily[source]

Bases: parce.lang.scheme.Scheme

Scheme used with LilyPond.

scheme

Pick one thing and pop back.

argument

One Scheme expression.

classmethod common(pop=0)[source]

Yield common stuff. pop can be set to -1 for one-arg mode.

scheme_number(tokens)[source]

Return the Python value of the Scheme number in the specified tokens iterable.

All tokens that can be in the Scheme.number() context are supported. Supports all features: nan, +/- inf, fractions, exactness, complex numbers and polar coordinates.

Raises ValueError or ZeroDivisionError on faulty input.

Usage example:

>>> text = '123'
>>> from parce import root
>>> for n in root(Scheme.root, text):
...     if n == Scheme.number:
...         value = scheme_number(n)
...
scheme_number_from_text(text)[source]

Proof-of-concept/test function parsing Scheme/Guile number syntax.

Usage:

>>> from parce.lang.scheme import scheme_number_from_text
>>> scheme_number_from_text('123')
123
>>> scheme_number_from_text('123+3i')
(123+3j)
>>> scheme_number_from_text('#x123')
291
>>> scheme_number_from_text('#o13')
11
>>> scheme_number_from_text('1/3')
Fraction(1, 3)
>>> scheme_number_from_text('#i1/3')
0.3333333333333333
>>> scheme_number_from_text('#xdead/beef')
Fraction(57005, 48879)

Raises ValueError or ZeroDivisionError on invalid input.

Example:

Root lexicon Scheme.root and text:

Text rendered using default theme

; scheme example
; convert to html entities
(define (attribute-escape s)
  (string-substitute "\n" "
"
    (string-substitute "\"" """
      (string-substitute "&" "&"
        s))))

Result tree:

<Context Scheme.root at 0-194 (3 children)>
 ├╴<Context Scheme.singleline_comment at 0-16 (2 children)>
 │  ├╴<Token ';' at 0:1 (Comment)>
 │  ╰╴<Token ' scheme example' at 1:16 (Comment)>
 ├╴<Context Scheme.singleline_comment at 17-43 (2 children)>
 │  ├╴<Token ';' at 17:18 (Comment)>
 │  ╰╴<Token ' convert to html entities' at 18:43 (Comment)>
 ╰╴<Context Scheme.list at 44-194 (5 children)>
    ├╴<Token '(' at 44:45 (Delimiter.OpenParen)>
    ├╴<Token 'define' at 45:51 (Keyword)>
    ├╴<Context Scheme.list at 52-72 (4 children)>
    │  ├╴<Token '(' at 52:53 (Delimiter.OpenParen)>
    │  ├╴<Token 'attribute-escape' at 53:69 (Name)>
    │  ├╴<Token 's' at 70:71 (Name)>
    │  ╰╴<Token ')' at 71:72 (Delimiter.CloseParen)>
    ├╴<Context Scheme.list at 75-193 (6 children)>
    │  ├╴<Token '(' at 75:76 (Delimiter.OpenParen)>
    │  ├╴<Token 'string-substitute' at 76:93 (Name)>
    │  ├╴<Context Scheme.string at 94-98 (3 children)>
    │  │  ├╴<Token '"' at 94:95 (Literal.String)>
    │  │  ├╴<Token '\\n' at 95:97 (Literal.String.Escape)>
    │  │  ╰╴<Token '"' at 97:98 (Literal.String)>
    │  ├╴<Context Scheme.string at 99-106 (3 children)>
    │  │  ├╴<Token '"' at 99:100 (Literal.String)>
    │  │  ├╴<Token '&#10;' at 100:105 (Literal.String)>
    │  │  ╰╴<Token '"' at 105:106 (Literal.String)>
    │  ├╴<Context Scheme.list at 111-192 (6 children)>
    │  │  ├╴<Token '(' at 111:112 (Delimiter.OpenParen)>
    │  │  ├╴<Token 'string-substitute' at 112:129 (Name)>
    │  │  ├╴<Context Scheme.string at 130-134 (3 children)>
    │  │  │  ├╴<Token '"' at 130:131 (Literal.String)>
    │  │  │  ├╴<Token '\\"' at 131:133 (Literal.String.Escape)>
    │  │  │  ╰╴<Token '"' at 133:134 (Literal.String)>
    │  │  ├╴<Context Scheme.string at 135-143 (3 children)>
    │  │  │  ├╴<Token '"' at 135:136 (Literal.String)>
    │  │  │  ├╴<Token '&quot;' at 136:142 (Literal.String)>
    │  │  │  ╰╴<Token '"' at 142:143 (Literal.String)>
    │  │  ├╴<Context Scheme.list at 150-191 (6 children)>
    │  │  │  ├╴<Token '(' at 150:151 (Delimiter.OpenParen)>
    │  │  │  ├╴<Token 'string-substitute' at 151:168 (Name)>
    │  │  │  ├╴<Context Scheme.string at 169-172 (3 children)>
    │  │  │  │  ├╴<Token '"' at 169:170 (Literal.String)>
    │  │  │  │  ├╴<Token '&' at 170:171 (Literal.String)>
    │  │  │  │  ╰╴<Token '"' at 171:172 (Literal.String)>
    │  │  │  ├╴<Context Scheme.string at 173-180 (3 children)>
    │  │  │  │  ├╴<Token '"' at 173:174 (Literal.String)>
    │  │  │  │  ├╴<Token '&amp;' at 174:179 (Literal.String)>
    │  │  │  │  ╰╴<Token '"' at 179:180 (Literal.String)>
    │  │  │  ├╴<Token 's' at 189:190 (Name)>
    │  │  │  ╰╴<Token ')' at 190:191 (Delimiter.CloseParen)>
    │  │  ╰╴<Token ')' at 191:192 (Delimiter.CloseParen)>
    │  ╰╴<Token ')' at 192:193 (Delimiter.CloseParen)>
    ╰╴<Token ')' at 193:194 (Delimiter.CloseParen)>