File: frontend.py

package info (click to toggle)
python-stone 3.3.9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,036 kB
  • sloc: python: 22,311; objc: 498; sh: 23; makefile: 11
file content (55 lines) | stat: -rw-r--r-- 1,734 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import logging

from .exception import InvalidSpec
from .parser import (
    ParserFactory,
)
from .ir_generator import IRGenerator

logger = logging.getLogger('stone.frontend.frontend')


# FIXME: Version should not have a default.
def specs_to_ir(specs, version='0.1b1', debug=False, route_whitelist_filter=None):
    """
    Converts a collection of Stone specifications into the intermediate
    representation used by Stone backends.

    The process is: Lexer -> Parser -> Semantic Analyzer -> IR Generator.

    The code is structured as:
        1. Parser (Lexer embedded within)
        2. IR Generator (Semantic Analyzer embedded within)

    :type specs: List[Tuple[path: str, text: str]]
    :param specs: `path` is never accessed and is only used to report the
        location of a bad spec to the user. `spec` is the text contents of
        a spec (.stone) file.

    :raises: InvalidSpec

    :returns: stone.ir.Api
    """

    parser_factory = ParserFactory(debug=debug)
    partial_asts = []

    for path, text in specs:
        logger.info('Parsing spec %s', path)
        parser = parser_factory.get_parser()
        if debug:
            parser.test_lexing(text)

        partial_ast = parser.parse(text, path)

        if parser.got_errors_parsing():
            # TODO(kelkabany): Show more than one error at a time.
            msg, lineno, path = parser.get_errors()[0]
            raise InvalidSpec(msg, lineno, path)
        elif len(partial_ast) == 0:
            logger.info('Empty spec: %s', path)
        else:
            partial_asts.append(partial_ast)

    return IRGenerator(partial_asts, version, debug=debug,
                       route_whitelist_filter=route_whitelist_filter).generate_IR()