3.2. Parser Module

3.2.1. Lexer Class

class aggregate.parser.UnderwritingLexer[source]

Implements the Lexer for the agg language.

static preprocess(program)[source]

Separate preprocessor step, allowing it to be called separately. Preprocessing involves six steps:

  1. Remove // comments, through end of line

  2. Remove n in [ ] (vectors) that appear from using f'{np.linspace(...)}'

  3. Backslash (line continuation) mapped to space

  4. nt is replaced with space, supporting the tabbed indented Portfolio layout

  5. Split on newlines

Parameters

program

Returns

3.2.2. Parser Class

class aggregate.parser.UnderwritingParser(safe_lookup_function, debug=False)[source]

Implements the Parser for the agg language.

Here are testers for the math expressions:

from aggregate import build
for t in ['-123', '-2%', '45%', '1e-3%', 'inf', '-inf', 'exp(1)', 'exp(1/2)', 'exp(-1)', '-1/8',
          'exp(10)/exp(3**2/2)', '2**10', '50/exp(.3**2/2)', '1/exp(1.9**2 / 2)']:
    a = build(t)
    print(a.name)
    assert float(a.name) == eval(t.replace('%', '/100').replace('exp', 'np.exp').replace('inf', 'np.inf'))

To test on the test_suite:

df = build.run_test_suite()
assert len(df.query('error != 0')) == 0
static enhance_debugfile(f_out='')[source]

Put links in the parser.out debug file, if DEBUGFILE != ‘’.

Parameters

f_out – Path or filename of output. If “” then DEBUGFILE.html used.

Returns

error(p)[source]

Default error handling function. This may be subclassed.

3.2.3. Remaining Functions

aggregate.parser.grammar(add_to_doc=False, save_to_fn='')[source]

Write the grammar at the top of the file as a docstring

To work with multi-rules enter them on one line, like so:

@_('builtin_agg PLUS expr', 'builtin_agg MINUS expr')
Parameters
  • add_to_doc – add the grammar to the docstring

  • save_to_fn – save the grammar to a file