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>
Every node type subclasses it.
Generates a code string from the node.
Returns: | a generated code string |
---|---|
Return type: | unicode |
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.
Generates a code string from the node.
Returns: | a generated code string |
---|---|
Return type: | unicode |
AtomicForm is self-evaluated Form.
atom ::= attribute_name | quote | number_literal | string_literal
Generates a code string from the node.
Returns: | a generated code string |
---|---|
Return type: | unicode |
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
Generates a code string from the node.
Returns: | a generated code string |
---|---|
Return type: | unicode |
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 |
---|
The symbol unicode string.
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 |
---|
Definition is called assignment also.
definition ::= form ( ":=" | "=" ) form
Parameters: |
---|
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'
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: |
|
---|
A string value in unicode.
The prefix characrer in str of one character. It may be None.
The dict that contains metacharacters (without leading slash) as keys and these meanings as values.
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 |
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 |
---|
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
Attribute is called message also.
attribute ::= form "." attribute_name | form ( index | block )
Parameters: |
|
---|
The AttributeName instance.
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:
Parameters: |
|
---|
The ArgumentList instance.
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 |
---|
Index is also called subscript.
index ::= "[" argument_list "]"
Parameters: | arguments (ArgumentList) – an argument list |
---|
The ArgumentList instance.
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 |
---|
The list of arguments.
Generator that yields argument names in unicode.
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 |
---|