Coder Social home page Coder Social logo

pylasu's People

Contributors

alessiostalla avatar ftomassetti avatar loradd avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

loradd

pylasu's Issues

Introduce the PylasuParser class

It could look similar to this:

class MyListener(ErrorListener):
    issues: list[Issue]
    issue_type: IssueType

    def __init__(self, issues: list[Issue], issue_type: IssueType):
        self.issues = issues
        self.issue_type = issue_type

    def syntaxError(self, recognizer, offendingSymbol, line, column, msg, e):
        self.issues.append(Issue(message=str(msg),
                                 type=self.issue_type, position=Position(start=Point(line=line, column=column),
                                                                         end=Point(line=line, column=column))))

    def reportAmbiguity(self, recognizer, dfa, startIndex, stopIndex, exact, ambigAlts, configs):
        pass

    def reportAttemptingFullContext(self, recognizer, dfa, startIndex, stopIndex, conflictingAlts, configs):
        pass

    def reportContextSensitivity(self, recognizer, dfa, startIndex, stopIndex, prediction, configs):
        pass


class PylasuParser:

    def __init__(lexer_class, parser_class):
        self.lexer_class= lexer_class
        self.parser_class = parser_class

    def parse(self, code: str) -> Result:
        issues = []

        input = InputStream(code)

        lexer = self.lexer_class(input)
        lexer.removeErrorListeners()
        lexer.addErrorListener(MyListener(issues, IssueType.LEXICAL))

        token_stream = CommonTokenStream(lexer)
        parser = self.parser_class(token_stream)
        parser.removeErrorListeners()
        parser.addErrorListener(MyListener(issues, IssueType.SYNTACTIC))
        parse_tree = parser.root_rule() # I am not sure how we can invoke the root rule through reflection

        ast = parse_tree.to_ast(issues)
        ast.assign_parents()

        return Result(root=ast, issues=issues)

Remove anonymous symbol resolution and simplify scope

See comment in #7.

@dataclass
class Scope:
    symbols: Dict[str, List[Symbol]] = field(default_factory=list)
    parent: Optional['Scope'] = field(default=None)

    def lookup(self, symbol_name: str, symbol_type: type = Symbol) -> Optional[Symbol]:
        return next((symbol for symbol in self.symbols.get(symbol_name, []) if isinstance(symbol, symbol_type)),
                    self.parent.lookup(symbol_name, symbol_type) if self.parent is not None else None)

    def add(self, symbol: Symbol):
        self.symbols[symbol.name] = self.symbols.get(symbol.name, []) + [symbol]

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.