summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniil Baturin <daniil@baturin.org>2019-06-30 04:03:51 +0700
committerDaniil Baturin <daniil@baturin.org>2019-06-30 04:03:51 +0700
commit7061cc0f5bf13ff66d91ec07e4788cb54a8407ed (patch)
treea3f5f58876fdfb9ebf6fec9af1c041b211f5a1b2
parentb280bab4edd89733d364e87adfb8a1e7e5f1b302 (diff)
downloadvyos1x-config-7061cc0f5bf13ff66d91ec07e4788cb54a8407ed.tar.gz
vyos1x-config-7061cc0f5bf13ff66d91ec07e4788cb54a8407ed.zip
T1479: switch to Menhir's incremental API and add support for reporting error line numbers.
-rw-r--r--src/parser.ml27
1 files changed, 27 insertions, 0 deletions
diff --git a/src/parser.ml b/src/parser.ml
new file mode 100644
index 0000000..67e41ba
--- /dev/null
+++ b/src/parser.ml
@@ -0,0 +1,27 @@
+open Util
+open Lexing
+
+module I = Vyos1x_parser.MenhirInterpreter
+
+let rec parse lexbuf (checkpoint : Config_tree.t I.checkpoint) =
+ match checkpoint with
+ | I.InputNeeded _env ->
+ let token = Vyos1x_lexer.token lexbuf in
+ let startp = lexbuf.lex_start_p
+ and endp = lexbuf.lex_curr_p in
+ let checkpoint = I.offer checkpoint (token, startp, endp) in
+ parse lexbuf checkpoint
+ | I.Shifting _
+ | I.AboutToReduce _ ->
+ let checkpoint = I.resume checkpoint in
+ parse lexbuf checkpoint
+ | I.HandlingError _env ->
+ let line, pos = Util.get_lexing_position lexbuf in
+ raise (Syntax_error (Some (line, pos), "Syntax error"))
+ | I.Accepted v -> v
+ | I.Rejected ->
+ raise (Syntax_error (None, "invalid syntax (parser rejected the input)"))
+
+let from_string s =
+ let lexbuf = Lexing.from_string s in
+ parse lexbuf (Vyos1x_parser.Incremental.config lexbuf.lex_curr_p)