1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3
4#
5# Copyright 2017, Data61
6# Commonwealth Scientific and Industrial Research Organisation (CSIRO)
7# ABN 41 687 119 230.
8#
9# This software may be distributed and modified according to the terms of
10# the BSD 2-Clause license. Note that NO WARRANTY is provided.
11# See "LICENSE_BSD2.txt" for details.
12#
13# @TAG(DATA61_BSD)
14#
15
16from __future__ import absolute_import, division, print_function, \
17    unicode_literals
18from camkes.internal.seven import cmp, filter, map, zip
19
20from .base import Parser as BaseParser
21from .stage0 import CPP, Reader
22from .stage1 import Parse1
23from .stage2 import Parse2
24from .stage3 import Parse3
25from .stage4 import Parse4
26from .stage5 import Parse5
27from .stage6 import Parse6
28from .stage7 import Parse7
29from .query import  QueryParseStage
30from .stage8 import Parse8
31from .stage9 import Parse9
32from .stage10 import Parse10
33import os
34
35class Parser(BaseParser):
36    def __init__(self, options=None):
37
38        # Build the file reader.
39        if hasattr(options, 'cpp') and options.cpp:
40            if hasattr(options, 'cpp_flag'):
41                flags = options.cpp_flag
42            else:
43                flags = []
44            s0 = CPP(options.cpp_bin, flags)
45        else:
46            s0 = Reader()
47
48        # Build the plyplus parser
49        s1 = Parse1(s0)
50
51        # Build the import resolver.
52        if hasattr(options, 'import_path'):
53            import_path = options.import_path
54        else:
55            import_path = []
56        s2 = Parse2(s1, import_path)
57
58        # Build the lifter.
59        s3 = Parse3(s2, debug=hasattr(options, 'verbosity') and
60            options.verbosity > 2)
61
62        # Build the reference resolver.
63        allow_forward = hasattr(options, 'allow_forward_references') and \
64            options.allow_forward_references
65        s4 = Parse4(s3, allow_forward)
66
67        # Build the group collapser.
68        s5 = Parse5(s4)
69
70        # Build the assembly combiner.
71        s6 = Parse6(s5)
72
73        # Build the component hierarchy flattener.
74        s7 = Parse7(s6)
75
76        # Build the Query resolver
77        if hasattr(options, 'queries'):
78            queries = options.queries
79        else:
80            queries = None
81        s71 = QueryParseStage(s7, queries)
82
83        # Build the attribute resolver.
84        s8 = Parse8(s71)
85
86        # Build the N-1 connection combiner.
87        s9 = Parse9(s8)
88
89        # Build the AST freezer.
90        s10 = Parse10(s9)
91
92        self.parser = s10
93
94    def parse_file(self, filename):
95        return self.parser.parse_file(filename)
96
97    def parse_string(self, string):
98        return self.parser.parse_string(string)
99
100def parse_file(filename, options=None):
101    p = Parser(options)
102    return p.parse_file(filename)
103
104def parse_string(string, options=None):
105    p = Parser(options)
106    return p.parse_string(string)
107