Skip to content

Commit

Permalink
Add basic support for Python (no documentation).
Browse files Browse the repository at this point in the history
  • Loading branch information
skvadrik committed Apr 3, 2024
1 parent bd0b0ba commit 95b916d
Show file tree
Hide file tree
Showing 69 changed files with 11,633 additions and 555 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
* text=auto
test/newlines/** -text
test/python/indentation_sensitive_crlf.re -text
13 changes: 13 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ option(RE2C_BUILD_LIBS "Build libraries" OFF)
option(RE2C_BUILD_RE2D "Build re2d executable (an alias for `re2c --lang d`)" ON)
option(RE2C_BUILD_RE2GO "Build re2go executable (an alias for `re2c --lang go`)" ON)
option(RE2C_BUILD_RE2OCAML "Build re2ocaml executable (an alias for `re2c --lang ocaml`)" ON)
option(RE2C_BUILD_RE2PY "Build re2py executable (an alias for `re2c --lang python`)" ON)
option(RE2C_BUILD_RE2RUST "Build re2rust executable (an alias for `re2c --lang rust`)" ON)

option(RE2C_BUILD_BENCHMARKS "Build benchmarks" OFF)
Expand Down Expand Up @@ -131,6 +132,7 @@ add_library(re2c_objects_autogen OBJECT
"${CMAKE_CURRENT_BINARY_DIR}/src/default_syntax_d.cc"
"${CMAKE_CURRENT_BINARY_DIR}/src/default_syntax_go.cc"
"${CMAKE_CURRENT_BINARY_DIR}/src/default_syntax_ocaml.cc"
"${CMAKE_CURRENT_BINARY_DIR}/src/default_syntax_python.cc"
"${CMAKE_CURRENT_BINARY_DIR}/src/default_syntax_rust.cc"
"${re2c_docs}"
)
Expand Down Expand Up @@ -214,6 +216,7 @@ re2c_bootstrap_syntax("include/syntax/c" "src/default_syntax_c.cc")
re2c_bootstrap_syntax("include/syntax/d" "src/default_syntax_d.cc")
re2c_bootstrap_syntax("include/syntax/go" "src/default_syntax_go.cc")
re2c_bootstrap_syntax("include/syntax/ocaml" "src/default_syntax_ocaml.cc")
re2c_bootstrap_syntax("include/syntax/python" "src/default_syntax_python.cc")
re2c_bootstrap_syntax("include/syntax/rust" "src/default_syntax_rust.cc")

# re2c
Expand All @@ -237,6 +240,12 @@ if (RE2C_BUILD_RE2OCAML)
target_compile_definitions(re2ocaml PUBLIC "RE2C_LANG=Lang::OCAML")
endif()

# re2py
if (RE2C_BUILD_RE2PY)
add_executable(re2py ${re2c_sources})
target_compile_definitions(re2py PUBLIC "RE2C_LANG=Lang::PYTHON")
endif()

# re2rust
if (RE2C_BUILD_RE2RUST)
add_executable(re2rust ${re2c_sources})
Expand Down Expand Up @@ -373,6 +382,9 @@ if (CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR)
if(RE2C_BUILD_RE2OCAML)
install(TARGETS re2ocaml RUNTIME DESTINATION bin)
endif()
if(RE2C_BUILD_RE2PY)
install(TARGETS re2py RUNTIME DESTINATION bin)
endif()
if(RE2C_BUILD_RE2RUST)
install(TARGETS re2rust RUNTIME DESTINATION bin)
install(FILES "${re2c_manpage_rust}" DESTINATION "share/man/man1")
Expand All @@ -382,6 +394,7 @@ if (CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR)
include/syntax/d
include/syntax/go
include/syntax/ocaml
include/syntax/python
include/syntax/rust
include/unicode_categories.re
DESTINATION "${RE2C_STDLIB_DIR}")
Expand Down
11 changes: 11 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ re2c_GEN_SRC = \
src/default_syntax_d.cc \
src/default_syntax_go.cc \
src/default_syntax_ocaml.cc \
src/default_syntax_python.cc \
src/default_syntax_rust.cc
re2c_GEN_HDR = \
src/options/parser.h \
Expand Down Expand Up @@ -180,6 +181,7 @@ re2c_BOOT = \
bootstrap/src/default_syntax_d.cc \
bootstrap/src/default_syntax_go.cc \
bootstrap/src/default_syntax_ocaml.cc \
bootstrap/src/default_syntax_python.cc \
bootstrap/src/default_syntax_rust.cc

# custom sources
Expand Down Expand Up @@ -323,6 +325,7 @@ dist_stdlib_DATA = \
include/syntax/d \
include/syntax/go \
include/syntax/ocaml \
include/syntax/python \
include/syntax/rust \
include/unicode_categories.re

Expand Down Expand Up @@ -548,6 +551,14 @@ re2ocaml_SOURCES = $(re2c_SOURCES)
nodist_re2ocaml_SOURCES = $(nodist_re2c_SOURCES)
endif

# re2py
if WITH_PYTHON
bin_PROGRAMS += re2py
re2py_CXXFLAGS = $(AM_CXXFLAGS) -DRE2C_LANG=Lang::PYTHON
re2py_SOURCES = $(re2c_SOURCES)
nodist_re2py_SOURCES = $(nodist_re2c_SOURCES)
endif

# re2rust
if WITH_RUST
bin_PROGRAMS += re2rust
Expand Down
187 changes: 187 additions & 0 deletions bootstrap/src/default_syntax_python.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
#include "src/parse/conf_parser.h"
const char* DEFAULT_SYNTAX_PYTHON =
"supported_apis = [\"generic\"];\n"
"// \"recursive_functions\" works syntactically, but causes stack overflows due to non-tail recusion\n"
"supported_code_models = [\"loop_switch\"];\n"
"supported_targets = [\"code\", \"dot\"];\n"
"supported_features = [\"nested_ifs\"];\n"
"\n"
"semicolons = 0;\n"
"implicit_bool_conversion = 0;\n"
"backtick_quoted_strings = 0;\n"
"standalone_single_quotes = 0;\n"
"indentation_sensitive = 1;\n"
"\n"
"conf:api = custom;\n"
"conf:api:style = free-form;\n"
"conf:api:sigil = \"@@\";\n"
"conf:header = \"\";\n"
"conf:eof = -1;\n"
"conf:sentinel = -1;\n"
"conf:yyfill:enable = 1;\n"
"conf:yyfill:parameter = 1;\n"
"conf:yyfill:check = 1;\n"
"conf:tags = 0;\n"
"conf:leftmost-captures = 0;\n"
"conf:posix-captures = 0;\n"
"conf:tags:prefix = \"yyt\";\n"
"conf:tags:expression = \"@@\";\n"
"conf:invert-captures = 0;\n"
"conf:define:YYBACKUP = \"YYBACKUP\";\n"
"conf:define:YYBACKUPCTX = \"YYBACKUPCTX\";\n"
"conf:define:YYCONDTYPE = \"yycondtype\";\n"
"conf:define:YYCTYPE = \"yyctype\";\n"
"conf:define:YYCTXMARKER = \"YYCTXMARKER\";\n"
"conf:define:YYCURSOR = \"YYCURSOR\";\n"
"conf:define:YYDEBUG = \"YYDEBUG\";\n"
"conf:define:YYFILL = \"YYFILL\";\n"
"conf:define:YYFILL@len = \"@@\";\n"
"conf:define:YYFILL:naked = 0;\n"
"conf:define:YYGETCONDITION = \"YYGETCONDITION\";\n"
"conf:define:YYGETCONDITION:naked = 0;\n"
"conf:define:YYGETSTATE = \"YYGETSTATE\";\n"
"conf:define:YYGETSTATE:naked = 0;\n"
"conf:define:YYLESSTHAN = \"YYLESSTHAN\";\n"
"conf:define:YYLIMIT = \"YYLIMIT\";\n"
"conf:define:YYMARKER = \"YYMARKER\";\n"
"conf:define:YYMAXFILL = \"YYMAXFILL\";\n"
"conf:define:YYMAXNMATCH = \"YYMAXNMATCH\";\n"
"conf:define:YYMTAGN = \"YYMTAGN\";\n"
"conf:define:YYMTAGP = \"YYMTAGP\";\n"
"conf:define:YYPEEK = \"YYPEEK\";\n"
"conf:define:YYRESTORE = \"YYRESTORE\";\n"
"conf:define:YYRESTORECTX = \"YYRESTORECTX\";\n"
"conf:define:YYRESTORETAG = \"YYRESTORETAG\";\n"
"conf:define:YYSETCONDITION = \"YYSETCONDITION\";\n"
"conf:define:YYSETCONDITION@cond = \"@@\";\n"
"conf:define:YYSETCONDITION:naked = 0;\n"
"conf:define:YYSETSTATE = \"YYSETSTATE\";\n"
"conf:define:YYSETSTATE:naked = 0;\n"
"conf:define:YYSETSTATE@state = \"@@\";\n"
"conf:define:YYSHIFT = \"YYSHIFT\";\n"
"conf:define:YYSHIFTSTAG = \"YYSHIFTSTAG\";\n"
"conf:define:YYSHIFTMTAG = \"YYSHIFTMTAG\";\n"
"conf:define:YYSKIP = \"YYSKIP\";\n"
"conf:define:YYSTAGN = \"YYSTAGN\";\n"
"conf:define:YYSTAGP = \"YYSTAGP\";\n"
"conf:define:YYFN = [\"<undefined-function-name>;<undefined-function-type>\"];\n"
"conf:yyfn:sep = \";\";\n"
"conf:variable:yyctable = \"yyctable\";\n"
"conf:variable:yyaccept = \"yyaccept\";\n"
"conf:variable:yytarget = \"yytarget\";\n"
"conf:variable:yystate = \"yystate\";\n"
"conf:variable:yych = \"yych\";\n"
"conf:variable:yych:conversion = 0;\n"
"conf:variable:yych:literals = hex;\n"
"conf:variable:yych:emit = 0;\n"
"conf:variable:yybm = \"yybm\";\n"
"conf:variable:yybm:hex = 0;\n"
"conf:variable:yystable = \"\"; // deprecated\n"
"conf:cond:abort = 0; // default case causes [redundant-case] warnings\n"
"conf:cond:prefix = \"yyc_\";\n"
"conf:cond:enumprefix = \"YYC_\";\n"
"conf:cond:divider = \"(* *********************************** *)\";\n"
"conf:cond:divider@cond = \"@@\";\n"
"conf:cond:goto = \"\";\n"
"conf:cond:goto@cond = \"@@\";\n"
"conf:state:abort = 1;\n"
"conf:state:nextlabel = 0;\n"
"conf:bit-vectors = 0;\n"
"conf:debug-output = 0;\n"
"conf:computed-gotos = 0;\n"
"conf:computed-gotos:threshold = 9;\n"
"conf:nested-ifs = 0;\n"
"conf:case-insensitive = 0;\n"
"conf:case-inverted = 0;\n"
"conf:case-ranges = 0;\n"
"conf:unsafe = 0;\n"
"conf:encoding:ebcdic = 0;\n"
"conf:encoding:utf32 = 0;\n"
"conf:encoding:ucs2 = 0;\n"
"conf:encoding:utf16 = 0;\n"
"conf:encoding:utf8 = 0;\n"
"conf:encoding-policy = ignore;\n"
"conf:empty-class = match-empty;\n"
"conf:indent:string = \" \";\n"
"conf:indent:top = 0;\n"
"conf:label:prefix = \"yy\";\n"
"conf:label:yyfill = \"\";\n"
"conf:label:yyloop = \"\";\n"
"conf:label:yyNext = \"\";\n"
"conf:label:start = 0;\n"
"\n"
"code:var_local = topindent name (have_init ? \" = \" init) nl;\n"
"code:var_global = code:var_local;\n"
"\n"
"code:const_local = topindent name \" = \" init nl;\n"
"code:const_global = code:const_local;\n"
"\n"
"// code:array_local\n"
"// code:array_global\n"
"code:array_elem = array \"[\" index \"]\";\n"
"\n"
"// code:type_int\n"
"// code:type_uint\n"
"// code:type_cond_enum\n"
"// code:type_yybm\n"
"// code:type_yytarget\n"
"\n"
"code:assign = [lhs: topindent lhs \" = \" rhs nl];\n"
"code:assign_op = topindent lhs \" = \" lhs \" \" op \" \" rhs nl;\n"
"\n"
"code:if_then_else =\n"
" [branch{0}: topindent \"if \" cond \":\" nl\n"
" indent [stmt: stmt] dedent]\n"
" [branch{1:-1}: topindent (have_cond? \"elif \" cond : \"else\") \":\" nl\n"
" indent [stmt: stmt] dedent];\n"
"\n"
"code:switch =\n"
" topindent \"match \" expr \":\" nl\n"
" indent [case: case] dedent;\n"
"\n"
"code:switch_cases = topindent \"case \"\n"
" [case{0:-2}: case \"|\"]\n"
" [case{-1}: case \":\" nl]\n"
" indent [stmt: stmt] dedent;\n"
"\n"
"code:switch_case_range = (multival\n"
" ? [val{0}: val] [val{1:-1}: \"|\" val]\n"
" : [val{0}: val]);\n"
"\n"
"code:switch_case_default = \"_\";\n"
"\n"
"code:loop =\n"
" topindent \"while True:\" nl\n"
" indent [stmt: stmt] dedent;\n"
"\n"
"code:enum = [elem: elem \" = \" init nl];\n"
"\n"
"code:enum_elem = name;\n"
"\n"
"code:fndef =\n"
" \"def \" name \"(\" [arg{0}: argname] [arg{1:-1}: \", \" argname] \"):\" nl\n"
" indent [stmt: stmt] dedent;\n"
"\n"
"code:fncall =\n"
" topindent (have_retval ? retval \" = \") name \"(\" [arg{0:-2}: arg \", \"] [arg{-1}: arg] \")\" nl;\n"
"\n"
"code:tailcall =\n"
" topindent (have_retval ? \"return \") name \"(\" [arg{0:-2}: arg \", \"] [arg{-1}: arg] \")\" nl;\n"
"\n"
"code:recursive_functions = [fn: fndef nl];\n"
"\n"
"code:fingerprint =\n"
" \"# Generated by re2c\" (version? \" \" version) (date? \" on \" date) nl;\n"
"\n"
"code:abort = topindent \"raise \\\"internal lexer error\\\"\" nl;\n"
"\n"
"code:yyaccept = topindent var \" = \" num nl;\n"
"\n"
"code:yydebug = topindent debug \" \" state \" \" char nl;\n"
"\n"
"code:yypeek = topindent char \" = \" peek (api_style.functions ? \"()\") nl;\n"
"\n"
"code:yyskip = topindent skip (api_style.functions ? \"()\") nl;\n"
"\n"
"code:yybackup = topindent backup (api_style.functions ? \"()\") nl;\n"
;
Loading

0 comments on commit 95b916d

Please sign in to comment.