-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit cae63ee
Showing
20 changed files
with
1,027 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
--- | ||
Language: Cpp | ||
BasedOnStyle: Google | ||
Standard: c++20 | ||
ColumnLimit: 0 | ||
Cpp11BracedListStyle: false | ||
AlignAfterOpenBracket: DontAlign | ||
AllowShortLambdasOnASingleLine: Empty | ||
BraceWrapping: | ||
BeforeLambdaBody: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
--- | ||
--- | ||
Checks: '-*, | ||
modernize-redundant-void-arg, | ||
modernize-replace-random-shuffle, | ||
modernize-shrink-to-fit, | ||
# modernize-use-auto, | ||
modernize-use-bool-literals, | ||
modernize-use-equals-default, | ||
# modernize-use-equals-delete, | ||
modernize-use-nullptr, | ||
modernize-use-override, | ||
# google-build-explicit-make-pair, | ||
google-explicit-constructor, | ||
google-readability-casting' | ||
WarningsAsErrors: '' | ||
HeaderFilterRegex: '' | ||
AnalyzeTemporaryDtors: false | ||
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Ensure Github detects our c++ code as c++ code | ||
/include/** linguist-language=C++ | ||
*.h linguist-language=C++ | ||
*.cpp linguist-language=C++ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
name: windows | ||
|
||
on: [push] | ||
|
||
jobs: | ||
build: | ||
runs-on: windows-latest | ||
strategy: | ||
matrix: | ||
build_type: [Debug, Release] | ||
architecture: [Win32, x64] | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- name: Configure | ||
run: cmake -B build -G "Visual Studio 17 2022" -A ${{ matrix.architecture }} -DCMAKE_BUILD_TYPE=${{ matrix.build_type }} | ||
- name: Build | ||
run: cmake --build build --config ${{ matrix.build_type }} --parallel 10 | ||
- name: Test | ||
working-directory: build | ||
run: ctest -C ${{ matrix.build_type }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
.vs/ | ||
.vscode/ | ||
.cache/ | ||
/build/ | ||
/debug/ | ||
/release/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
cmake_minimum_required (VERSION 3.8) | ||
|
||
project ("parser-combinator") | ||
|
||
set(CMAKE_CXX_STANDARD 20) | ||
|
||
include(cmake/cpm.cmake) | ||
|
||
enable_testing() | ||
add_subdirectory("tests") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2023 xiaowen | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# Parser Combinator | ||
[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/axiaowen/parser-combinator/blob/master/LICENSE.MIT) | ||
[![GitHub Actions Status](https://github.com/axiaowen/parser-combinator/workflows/windows/badge.svg?branch=master)](https://github.com/axiaowen/parser-combinator/actions) | ||
|
||
An experimental parser combinator library written in C++23. Fully support `constexpr` parsing in compile time. | ||
|
||
## Example | ||
```C++ | ||
constexpr auto ipv4 = []( | ||
uint8_t a, uint8_t b, uint8_t c, uint8_t d) { | ||
return (a << 24) | (b << 16) | (c << 8) | d; | ||
}; | ||
|
||
constexpr auto octet = integer<uint8_t>; | ||
constexpr auto ipv4_addr = eof(sepby<4>(octet, dot)); | ||
constexpr auto to_ipv4 = ipv4_addr | to([](auto&& result) { | ||
auto [a, b, c, d] = result; | ||
return ipv4( a, b, c, d ); | ||
}); | ||
static_assert(to_ipv4("192.168.1.1") == ipv4(192, 168, 1, 1)); | ||
``` | ||
## Monadic | ||
* **to** converts the parser's result to anther type | ||
* **map** maps a function over the result of a parser | ||
* **or_else** applies a function over the input if the parser failed | ||
* **and_then** applies a function over the result of a parser | ||
## Combinator | ||
* **success** consumes no input and always succeeds with given value. | ||
* **predict** return the result of the parser if it satisfies a predictate | ||
* **seq (operator+)** matches a sequence of parsers in the defined order. Return a std::tuple of the two return value of the parsers. | ||
* **choice (operator||)** tries to apply the parsers in order until one of them succeeds. | ||
* **left** matches two parsers and accepts the result from the left side. | ||
* **right** matches two parsers and accepts the result from the right side. | ||
* **between** matches three parsers and accepts the result from the middle one. | ||
* **many** matches a parser multiple times, can be a matched 0 times. Return a std::vector of the return value of the parser. | ||
* **many1** matches a parser multiple times at least one time. Return a std::vector of the return value of the parser. | ||
* **many<>** matches a parser in a fixed amount of times. Return a std::array of the return value of the parser. | ||
* **sepby** matches a parser separated by another parser, can be a matched 0 times. | ||
* **sepby1** matches a parser separated by another parser at least one time. | ||
* **sepby<>** matches a parser separated by another parser in a fixed amount of times. | ||
* **eof** matches the of the input | ||
## Character | ||
* **any** matches any character | ||
* **satisfy** matches one character if it satisfies a predicate. | ||
* **range** matches one character in the range of characters. | ||
* **one_of** matches the character in the list of characters. | ||
* **none_of** matches the character not in the list of characters. | ||
* **digit** matches one numerical character: 0-9. | ||
* **octdigit** matches one octal numerical character: 0-7. | ||
* **hexdigit** matches one hexadecimal numerical character: 0-9, a-f, A-F. | ||
* **lower** matches one lowercase alphabetic character: a-z. | ||
* **upper** matches one uppercase alphabetic character: A-Z. | ||
* **alpha** matches one alphabet character: a-z, A-Z. | ||
* **alphanum** matches one numerical or alphabetic character: 0-9, a-z, A-Z. | ||
* **sign** matches one sign character: -, +. | ||
* **space** matches one whitespace character | ||
* **dot** matches one semi character: .. | ||
* **semi** matches one semi character: ;. | ||
* **comma** matches one comma character: ,. | ||
* **colon** matches one colon character: :. | ||
* **quota** matches one quota character: ". | ||
* **escape** matches one escaped character: \", \\, \/, \b, \f, \n, \r, \t. | ||
## Token | ||
* **symbol** matches a specific string. | ||
* **octal** matches a octal number. | ||
* **decimal** matches a decimal number. | ||
* **hexadecimal** matches a hexadecimal number. | ||
* **squares** matches a parser enclosed in squares: []. | ||
* **brackets** matches a parser enclosed in brackets: {}. | ||
* **parentheses** matches a parser enclosed in parentheses: (). | ||
## Compiler support | ||
* MSVC 19.34+ /std::c++latest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
set(CPM_DOWNLOAD_VERSION 0.38.1) | ||
|
||
if(CPM_SOURCE_CACHE) | ||
set(CPM_DOWNLOAD_LOCATION "${CPM_SOURCE_CACHE}/cpm/CPM_${CPM_DOWNLOAD_VERSION}.cmake") | ||
elseif(DEFINED ENV{CPM_SOURCE_CACHE}) | ||
set(CPM_DOWNLOAD_LOCATION "$ENV{CPM_SOURCE_CACHE}/cpm/CPM_${CPM_DOWNLOAD_VERSION}.cmake") | ||
else() | ||
set(CPM_DOWNLOAD_LOCATION "${CMAKE_BINARY_DIR}/cmake/CPM_${CPM_DOWNLOAD_VERSION}.cmake") | ||
endif() | ||
|
||
# Expand relative path. This is important if the provided path contains a tilde (~) | ||
get_filename_component(CPM_DOWNLOAD_LOCATION ${CPM_DOWNLOAD_LOCATION} ABSOLUTE) | ||
|
||
function(download_cpm) | ||
message(STATUS "Downloading CPM.cmake to ${CPM_DOWNLOAD_LOCATION}") | ||
file(DOWNLOAD | ||
https://github.com/cpm-cmake/CPM.cmake/releases/download/v${CPM_DOWNLOAD_VERSION}/CPM.cmake | ||
${CPM_DOWNLOAD_LOCATION} | ||
) | ||
endfunction() | ||
|
||
if(NOT (EXISTS ${CPM_DOWNLOAD_LOCATION})) | ||
download_cpm() | ||
else() | ||
# resume download if it previously failed | ||
file(READ ${CPM_DOWNLOAD_LOCATION} check) | ||
if("${check}" STREQUAL "") | ||
download_cpm() | ||
endif() | ||
unset(check) | ||
endif() | ||
|
||
include(${CPM_DOWNLOAD_LOCATION}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#pragma once | ||
#include <algorithm> | ||
#include <ranges> | ||
|
||
#include "combinator.h" | ||
|
||
namespace parsec { | ||
|
||
// matches any char | ||
constexpr auto any() { | ||
using R = ParserResult<char>; | ||
return [](const Input& input) { | ||
if (input.empty()) { | ||
return R{ std::unexpect, "input is empty" }; | ||
} | ||
return R{ { input.at(0), input.substr(1) } }; | ||
}; | ||
}; | ||
|
||
template <class F> | ||
constexpr auto satisfy(F&& f) { | ||
return predict(any(), f); | ||
} | ||
|
||
template <char begin, char end> | ||
constexpr auto range = satisfy([](char c) { | ||
return begin <= c && c <= end; | ||
}); | ||
|
||
template <char... chs> | ||
constexpr auto one_of = satisfy([](char c) { | ||
constexpr char arr[] = { chs... }; | ||
return std::ranges::contains(arr, c); | ||
}); | ||
|
||
template <char... chs> | ||
constexpr auto none_of = satisfy([](char c) { | ||
constexpr char arr[] = { chs... }; | ||
return !std::ranges::contains(arr, c); | ||
}); | ||
|
||
constexpr auto digit = range<'0', '9'>; | ||
constexpr auto octdigit = range<'0', '7'>; | ||
constexpr auto hexdigit = range<'0', '9'> || range<'A', 'F'> || range<'a', 'f'>; | ||
constexpr auto lower = range<'a', 'z'>; | ||
constexpr auto upper = range<'A', 'Z'>; | ||
constexpr auto alpha = lower || upper; | ||
constexpr auto alphanum = alpha || digit; | ||
constexpr auto sign = one_of<'+', '-'>; | ||
constexpr auto space = one_of<' ', '\n', '\r', '\t'>; | ||
constexpr auto dot = one_of<'.'>; | ||
constexpr auto semi = one_of<';'>; | ||
constexpr auto comma = one_of<','>; | ||
constexpr auto colon = one_of<':'>; | ||
constexpr auto quota = one_of<'\"'>; | ||
|
||
} // namespace parsec |
Oops, something went wrong.