Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Cairn.Parsing
SourceThis module main use is to create a parser that can log its execution and is able to signal several errors (with the right option) from a parser generated with menhir.
The functor Make produces this parser provided it is given the modules generated by menhir, along the one obtained from reading the .cmly file generated by menhir.
A typical instantiation of this module should look like :
module Grammar = MenhirSdk.Cmly_read.Read (struct
let filename = "Parser.cmly"
end)
module P =
Cairn.Parsing.Make
(struct
type value_parsed = Program.program
end)
(Parser)
(Lexer)
(ParserMessages)
(Grammar)
assuming Lexer
, Parser
and ParserMessages
are the modules produced by menhir (with the right options), and that "Parser.cmly" is the name (with path) to the cmly file produced by menhir. It is assumed that the parsing function of Parser.Incremental
is main
, and the lexing function of lexer is token
. If it isn't the case, you need to tweak the corresponding modules to make it so.
For the cmly file, it might not straightforward to use its direct name (especially if the executable is destined to be installed or executed from somewhere else than its own directory).
In that case, it might be worth to bundle it in the executable with, for example, ocaml-crunch (see examples provided to see how). It is then needed to use the FromString
functor of MenhirSdk.Cmly_read
rather than the Read
one as follows:
module Grammar = MenhirSdk.Cmly_read.FromString (struct
let content = Option.get (<Module_generated_by_ocaml_crunch>.read "<name_of_cmly_file>")
end)
If you are running a version of Menhir anterior to 2023/12/31, then the FromString functor is not exposed, and you have instead to reproduce its behavior with the Lift
functor as follows:
module Grammar = MenhirSdk.Cmly_read.Lift (struct
let file_content = Option.get (<Module_generated_by_ocaml_crunch>.read "<name_of_cmly_file>")
let prefix = "CMLY" ^ MenhirSdk.Version.version
let grammar = Marshal.from_string file_content (String.length prefix)
end)
This is adapted from the Read
functor of MenhirSdk.Cmly_read
.
type error_strategy =
| Stop
The parser will stop after the first error encountered.
*)| PopFirst
After an error, the parser will pop the stack until either the top element is a terminal or non-terminal with backup attribute set (in the grammar) or the stack is empty, and then ignore tokens until the first that can be shifted.
*)type for error-recovery strategy
Technical module containing the signature needed and provided by Cairn, in order to make them generic for the type of value returned by the parser.
Signature matching the module generated by menhir with option --compile-errors. Used for error displaying. Will work if provided with a dummy message
function instead (but less informative).
module Make
(T : sig ... end)
(Parser : P(T).parser)
(Lexer : sig ... end)
(ParserMessages : parser_messages)
(Grammar : MenhirSdk.Cmly_api.GRAMMAR) :
P(T).parser_logger
Main functor of this module. It generates a module that can parse a text with the parser provided as an argument, and generates a log of the partial derivations produced along the run of the parser, a log of errors encountered (several errors supported if generated with PopFirst strategy), and can display a tui explorer of the sequence of partial derivations produced by the parser.
module MakeWithDefaultMessage
(T : sig ... end)
(Parser : P(T).parser)
(Lexer : sig ... end)
(Grammar : MenhirSdk.Cmly_api.GRAMMAR) :
P(T).parser_logger
Same as Make, but provides defaults error messages of the form "Error on state x" where x is the state where the parser encountered the message. Useful if you do not want to generate messages with menhir (yet).