From 3b3a8c1c924f4db9b27f7ee740ffc16f5d0b4eb9 Mon Sep 17 00:00:00 2001 From: Erez Sh Date: Fri, 26 Jun 2020 00:37:37 +0300 Subject: [PATCH] Added docs for on_error --- docs/classes.md | 15 +++++++++++++-- docs/features.md | 1 + lark/lark.py | 2 +- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/docs/classes.md b/docs/classes.md index 60d08ef..084cda6 100644 --- a/docs/classes.md +++ b/docs/classes.md @@ -25,12 +25,21 @@ Example: Lark(...) ``` -#### parse(self, text) +#### parse(self, text, start=None, on_error=None) -Return a complete parse tree for the text (of type Tree) +Parse the given text, according to the options provided. + +Returns a complete parse tree for the text (of type Tree) If a transformer is supplied to `__init__`, returns whatever is the result of the transformation. +Parameters: + +* start: str - required if Lark was given multiple possible start symbols (using the start option). + +* on_error: function - if provided, will be called on UnexpectedToken error. Return true to resume parsing. LALR only. + +(See `examples/error_puppet.py` for an example of how to use `on_error`.) #### save(self, f) / load(cls, f) @@ -160,6 +169,8 @@ See the [visitors page](visitors.md) ## UnexpectedToken +TODO: Explain puppet mechanism (related to on_error) + ## UnexpectedException - `UnexpectedInput` diff --git a/docs/features.md b/docs/features.md index 5dff9f4..d8a4340 100644 --- a/docs/features.md +++ b/docs/features.md @@ -6,6 +6,7 @@ - EBNF-inspired grammar, with extra features (See: [Grammar Reference](grammar.md)) - Builds a parse-tree (AST) automagically based on the grammar - Stand-alone parser generator - create a small independent parser to embed in your project. + - Flexible error handling by using a "puppet parser" mechanism (LALR only) - Automatic line & column tracking (for both tokens and matched rules) - Automatic terminal collision resolution - Standard library of terminals (strings, numbers, names, etc.) diff --git a/lark/lark.py b/lark/lark.py index f5d957e..f7d12fc 100644 --- a/lark/lark.py +++ b/lark/lark.py @@ -364,7 +364,7 @@ class Lark(Serialize): Parameters: start: str - required if Lark was given multiple possible start symbols (using the start option). - on_error: function - if provided, will be called on UnexpectedToken error. Return true to resume parsing. + on_error: function - if provided, will be called on UnexpectedToken error. Return true to resume parsing. LALR only. Returns a tree, unless specified otherwise. """