A parser generator.
The workflow: Markdown ===(Python)==> C++11.
- Run
python myparser.py c++ <filename>.md
.
- Import
myparser
andmyparser_cpp
in python. - Create a parser object:
parser = myparser.MyParser()
. - Load a syntax file:
parser.add_file(filename)
. - Generate the parser:
myparser_cpp.cplusplus_gen_auto(parser, './', 'SYNTAX_HPP')
. - Save the generated code as a C++ header file.
- Import the generated code and use the namespace
myparser
. - Parse a file:
auto ast = Parser<>::parseFile(filename)
, or a string:Parser<>::parse(text)
. - Visit the AST via RTTI, or use a visitor
myparser::Pass<>
:ast->runPass(&pass)
. - Compile your code with
-std=c++11
.
MyParser accepts a special Markdown ("MyParser Markdown") file.
The README.md
(this file) is not only a document but also an example of a syntax file.
You could run python self_syntax.py
or python myparser.py c++ README.md -o self_syntax.hpp
. That loads README.md
and generates self_syntax.hpp
.
Then, just compile self_bootstrap.cpp
. A syntax highlighter that accepts MyParser Markdown files as its input is prepared now. You could see README.md
printed with syntax highlights.
root:
<> <blocks> <>
space:
<*comment line>
keyword:
<id>
<sign>
blocks:
<+block>
block:
<list head> <list body> <>
<builtin head> <list body> <>
<regex head> <regex line> <>
list head:
<id>:<line break>
builtin head:
**<id>**:<line break>
regex head:
*<id>*:<line break>
list body:
<list line> <list body>
<list line>
list line:
<indent><list items><line break>
regex line:
<indent><anything><line break>
comment line:
<indent>//<anything><line break>
<other markdown><line break>
<line break>
list items:
<*list item>
list item:
\ <>
<empty item>
<keyword item>
<ref item>
<error item>
empty item:
\<<>\>
keyword item:
([^ \r\n\\<]|\\.)+
ref item:
\<<id>\>
\<?<id>\>
\<*<id>\>
\<+<id>\>
error item:
\<!<id>\>
id:
\w([\w ]*\w)?
sign:
\**:|\*+| +|\/\/|<[\*\+\?\!]?|>
other markdown:
( ? ? ?)[^ \r\n].*[^:\r\n]|.|
indent:
( )
line break:
[\r\n]+
anything:
.*