comedy.node — Abstract syntax tree

This module provides AST node types.

alpha      ::=  "A" | "B" | "C" | "D" | "E" | "F" | "G" | "H" | "I" | "J" | "K" |
                "L" | "M" | "N" | "O" | "P" | "Q" | "R" | "S" | "T" | "U" | "V" |
                "W" | "X" | "Y" | "Z" |
                "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" | "j" | "k" |
                "l" | "m" | "n" | "o" | "p" | "q" | "r" | "s" | "t" | "u" | "v" |
                "w" | "x" | "y" | "z"
digit      ::=  "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
newline    ::=  "\n"
whitespace ::=  " " | "\t" | "\r" | newline
any_char   ::=  <any unicode character>
class comedy.node.Node

Every node type subclasses it.

generate_code_string()

Generates a code string from the node.

Returns:a generated code string
Return type:unicode
class comedy.node.Form

Form is also called expression also. It can be evaluated always.

form ::=  "(" form ")" |
          atom | attribute | call | definition

It is just an abstract base class; should be subclassed.

generate_code_string()

Generates a code string from the node.

Returns:a generated code string
Return type:unicode
class comedy.node.AtomicForm

AtomicForm is self-evaluated Form.

atom ::=  attribute_name | quote | number_literal | string_literal
generate_code_string()

Generates a code string from the node.

Returns:a generated code string
Return type:unicode
class comedy.node.AttributeName

AttributeName is the Form that can be used as a name of Attribute. It is an abstract base class, so should be subclassed.

attribute_name ::=  symbol | index | block
generate_code_string()

Generates a code string from the node.

Returns:a generated code string
Return type:unicode
class comedy.node.Symbol(symbol)

Symbol is called variable, identifier or name also. It is evaluated to the assigned object in the comedy.runtime.Scope with it.

symbol   ::=  ( sym_char - ( digit | "=" ) ) sym_char* |
              "=" sym_char+
sym_char ::=  any_char - ( whitespace | ":" | "." | "," | ";" | "(" |
                             ")" | "[" | "]" | "{" | "}" | '"' | "'")

Symbols of the same name are the same:

>>> Symbol(u'abc') is Symbol(u'abc')
True

A ValueError rises when the invalid symbol string has given:

>>> Symbol(u'f(x)')
Traceback (most recent call last):
  ...
ValueError: invalid symbol: u'f(x)'

Takes an unicode string only. A TypeError rises when any else has given:

>>> Symbol(['id'])
Traceback (most recent call last):
  ...
TypeError: symbol must be an unicode string, not ['id']

However a str becomes an unicode string:

>>> Symbol('str')
comedy.node.Symbol(u'str')

For the Symbol instance, the constructor behaves as identity function:

>>> Symbol(Symbol('id'))
comedy.node.Symbol(u'id')
Parameters:symbol (basestring, Symbol) – an symbol string. if a Symbol instance has given, returns it
symbol

The symbol unicode string.

PATTERN

Regular expression (re) pattern that matches to symbol unicode strings.

POOL

A dict of unicode keys and Symbol values.

class comedy.node.Quote(form)

Quote a form.

quote ::=  ":(" form ")" |
           ":" symbol

Quotes for the same symbols are the same:

>>> q1 = Quote(Symbol('abc'))
>>> q2 = Quote(Symbol('abc'))
>>> q1 is q2
True
Parameters:form (Form) – a form to quote
form

The quoted Form instance.

POOL

A dict of unicode keys and Quote values. Only quotes for Symbol are stored in this dictionary.

class comedy.node.Definition(name, value, local=False)

Definition is called assignment also.

definition ::=  form ( ":=" | "=" ) form
Parameters:
  • name (Form) – a lvalue form
  • value (Form) – a rvalue form
  • local (bool) – whether it is a local definition (see attribute local also)
name

The Form instance of the lvalue.

value

The Form instance of the rvalue.

local

The bool value that represents whether it is a local definition. That is, True for := and False for =.

>>> Definition(name=Symbol(u'var'), value=Symbol(u'val'),
...   local=True).generate_code_string()
u'var := val'
>>> Definition(name=Symbol(u'var'), value=Symbol(u'val'),
...   local=False).generate_code_string()
u'var = val'
class comedy.node.StringLiteral(string, prefix=None)

String literal to be self-evaluated.

string_literal         ::=  [string_literal_prefix] | string_literal_body
string_literal_prefix  ::=  alpha
string_literal_body    ::=  '"' string_item* '"' |
                            "'" string_item* "'" |
                            '"""' string_item* '"""' |
                            "'''" string_item* "'''"
string_item            ::=  string_character | string_escape_sequence
string_character       ::=  <any source character except "\" or the quote>
string_escape_sequence ::=  "\" <any character>
Parameters:
  • string (basestring) – a body of the string literal
  • prefix (basestring) – a prefix character of the string literal. default is None
string

A string value in unicode.

prefix

The prefix characrer in str of one character. It may be None.

PATTERN

The re pattern that matches to string literals.

ESCAPE_PATTERN

The re pattern that matches to escaping sequences.

ESCAPE_CHARACTERS

The dict that contains metacharacters (without leading slash) as keys and these meanings as values.

generate_code_string()

Generates a string literal representation:

>>> c = StringLiteral.generate_code_string
>>> print c(StringLiteral(u'hello world'))
"hello world"
>>> print c(StringLiteral(u'hello', prefix='a'))
a"hello"
>>> print c(StringLiteral(u"'hello world'"))
"'hello world'"
>>> print c(StringLiteral(u'"hello world"'))
'"hello world"'
>>> print c(StringLiteral(u'"MinHee\'s implementation"'))
"\"MinHee's implementation\""
>>> print c(StringLiteral(u'\a\b\f\n\r\t\v'))
"\a\b\f\n\r\t\v"
>>> print c(StringLiteral(u'\xa9 Stark Industries'))
"\xa9 Stark Industries"
>>> print c(StringLiteral(u'\uac00\ub098\ub2e4'))
"\uac00\ub098\ub2e4"
>>> print c(StringLiteral(u'"hello world\n"'))
"\"hello world\n\""
>>> print c(StringLiteral(u'"\xa9 Stark Industries"'))
"\"\xa9 Stark Industries\""
Returns:the string literal representation
Return type:unicode
class comedy.node.NumberLiteral(number)

Number literal to be self-evaluated.

number_literal   ::=  integer_literal | float_literal
integer_literal  ::=  ["-"] digit+
float_literal    ::=  integer_literal "." digit+ |
                      exponent_literal
exponent_literal ::=  integer_literal ["." digit+]
                      ["e" | "E"] ["+" | "-"] digit+

Examples:

123        # <integer> 123
-123       # <integer> -123
3.14       # <float>   3.14
-3.14      # <float>   -3.14
10e+4      # <float>   100000.0
10e-4      # <float>   0.001
-10e+4     # <float>   -100000.0
-10e-4     # <fliat>   -0.001
3.141e+4   # <float>   31410.0
3.141e-4   # <float>   0.0003141
-3.141e+4  # <float>   -31410.0
-3.141e-4  # <float>   -0.0003141
Parameters:number (int, long, float, basestring) – a number
number

Python int, long or float value that it represents.

>>> NumberLiteral(123).number
123
>>> NumberLiteral(-3.141e+4).number
-31410.0
>>> NumberLiteral('3.14').number  
3.14...
>>> NumberLiteral('-0.1e+3').number
-100.0
PATTERN

The re pattern that matches to number literals.

INTEGER_PATTERN

The re pattern that matches to integral number literals.

FLOAT_PATTERN

The re pattern that matches to floating point number literals.

class comedy.node.Attribute(receiver, attribute)

Attribute is called message also.

attribute ::=  form "." attribute_name |
               form ( index | block )
Parameters:
  • receiver (Form) – the receiver of the message
  • attribute (AttributeName) – the attribute name
receiver

The Form instance that is the receiver of the message.

attribute

The AttributeName instance.

class comedy.node.Call(function, arguments)

Call is also called application or function call.

call            ::=  form "(" argument_list ")" |
                     form ":" unnamed_argument_list |
                     binary_operator
binary_operator ::=  form symbol form

There are three types of application forms:

normal form
Traditional C-style application form e.g. function(arguments).
simple form
Application form using only beginning colon character instead of parentheses pair e.g. function: arguments. There’s a limit that named arguments cannot be applicable for simple forms.
binary operator form
Binary operator-like style application form e.g. receiver attribute argument. There’s a limit that only one positional argument can be applicable for binary operator forms.
Parameters:
  • function (Form) – the function to apply
  • arguments (ArgumentList) – an argument list
function

The Form instance of the function body to apply.

arguments

The ArgumentList instance.

class comedy.node.Block(program)

Block is a special-purpose application form; it takes a program that contains zero or more forms.

block ::=  "{" program "}"
Parameters:program (Program, iterable object) – a program, an iterable object that yields Form
program

The Program instance of the block body.

class comedy.node.Index(arguments)

Index is also called subscript.

index ::=  "[" argument_list "]"
Parameters:arguments (ArgumentList) – an argument list
arguments

The ArgumentList instance.

class comedy.node.ArgumentList(arguments=[])

The list of arguments.

argument_list         ::=  [ argument ( "," argument )* [","] ]
unnamed_argument_list ::=  [ form ( "," form )* [","] ]
argument              ::=  [symbol ":"] form

The constructor takes an iterable object like list that contains (name, value) pairs or values only in Form instances:

>>> args = ArgumentList([(Symbol('name'), Symbol('value')),
...               (None, Symbol('value2'))])
>>> args  
comedy.node.ArgumentList([(u'name', comedy.node.Symbol(u'value')),
                          comedy.node.Symbol(u'value2')])
>>> print unicode(args)
name: value, value2

ArgumentList is iterable also:

>>> list(args)  
[(comedy.node.Symbol(u'name'), comedy.node.Symbol(u'value')),
 (None, comedy.node.Symbol(u'value2'))]
Parameters:arguments (iterable object) – an iterable object that contains (name, value) pairs and/or values only in Form instances. name, the first value of each pair must be string or Symbol instance. value, the second value of each pair must be a Form. default is empty
arguments

The list of arguments.

names

Generator that yields argument names in unicode.

symbols

Generator that yields argument names in Symbol.

values

Generator that yields argument values in Form.

class comedy.node.Program(forms=[])

Program is a section of code which is grouped together. It is a list of Form.

program        ::=  form_separator* ( form form_separator+ )*
                    form_separator*
form_separator ::=  ";" | newline
Parameters:forms (iterable object) – an iterable object that contains Form instances. default is empty
forms

The list of Form instances.

Project Versions

Previous topic

comedy — A naive Veeyu implementation

Next topic

comedy.parser — Veeyu parser

This Page