Enum Language
The Enum Language is a specification for building very complex enumerations that have support for many kind of structures. The language use indentation to delimit blocks and is parsed with the makeobj.parse
function.
The function will work with files and multiline strings.
I uses a set of keywords preceded by an @
symbol and they always start the line.
They are:
@obj, @keys, @attr, @method, @sub, @default, @set
This block must be at the top level of the file and each file may contain may of these. Inside this block the enumeration is defined.
In this block you define all the enumeration elements. The name are passed in a list (Python syntax or multiline block -- see the Block section)
This is used to create attributes specific for each element or attributes with the a default value that can be changed for some elements. Pass a list or a block with @default and @set for each of these behaviors.
Creates a function in the class that will be available for the instances. For now, the function can only return the body provided when called with an element.
This is one of the things that makes MakeObj so powerful. The elements attributes may have not only simple values but they may also have many levels of attributes.
@obj Test =:
@keys = 'a', 'b'
@sub x =:
@attr y = 1, 2
So you can use it like this:
Test.a.x.y == 1
Test.b.x.y == 2
This keyword is used inside the @attr
body to tell the default value of the name and this value will be the same for every element unless @set
is used.
Check the =:
symbol in the block session to see how can this keyword be avoided for simple cases.
Sets a value for an attribute with default value on a specific element
attr test =:
@default = 1
@set =>
'a': 2
Blocks are delimited by a line with a certain signature and a body:
@[keyword]: [name] =
[body]
The colon is optional when there's a name and symbol in the end of the line will define the expected block. If the block is small enough, it can stay in the same line as the signature.
The body of the block is defined by the symbol at the end of the signature. It can be one of these
=
=:
=>
This mean the following block is either a single line of valid Python data (parsed with ast
module) or various lines of Python data that will be joined in a list.
@attr value = ['hello', 1]
@attr value = 12345
@attr value =
'hello'
1234
'one', 'more', 'thing'
This symbol tells the parser the block contain more blocks inside its body when using the multiline form. When the content of the body is after the symbol, it treats as if it were a @default
value
@obj MyObj =:
@keys = 'a', 'b', c
@attr x =: 1
When you have to set specific names, this symbol can be used for create a pair key: value
on each line that will be added to a dictionary.
@attr value =:
@default = 1.0
@set =>
'a': 0.0
'b': 0.5