fix missing requirements and add test caller
This commit is contained in:
parent
d93ab67edc
commit
78ceb46b2e
8 changed files with 94 additions and 149 deletions
3
.vscode/settings.json
vendored
3
.vscode/settings.json
vendored
|
@ -1,4 +1,5 @@
|
||||||
{
|
{
|
||||||
"python.analysis.typeCheckingMode": "basic",
|
"python.analysis.typeCheckingMode": "basic",
|
||||||
"python.analysis.autoImportCompletions": true
|
"python.analysis.autoImportCompletions": true,
|
||||||
|
"python.pythonPath": "./venv/bin/python"
|
||||||
}
|
}
|
1
requirements.txt
Normal file
1
requirements.txt
Normal file
|
@ -0,0 +1 @@
|
||||||
|
okipy
|
3
setup.sh
3
setup.sh
|
@ -3,4 +3,5 @@ set -e
|
||||||
cd "$(dirname "$(realpath "$0")")"
|
cd "$(dirname "$(realpath "$0")")"
|
||||||
|
|
||||||
python3 -m venv venv
|
python3 -m venv venv
|
||||||
source venv/bin/activate
|
source venv/bin/activate
|
||||||
|
pip install -r requirements.txt
|
70
tests/cases.py
Executable file
70
tests/cases.py
Executable file
|
@ -0,0 +1,70 @@
|
||||||
|
#!/bin/env -S python
|
||||||
|
|
||||||
|
from typing import TypeVar, Any
|
||||||
|
from os.path import dirname
|
||||||
|
import sys
|
||||||
|
|
||||||
|
sys.path.append(f"{dirname(__file__)}/../src")
|
||||||
|
from pyalibert import regex, Parser, just, Declare
|
||||||
|
|
||||||
|
|
||||||
|
from okipy.lib import Suite
|
||||||
|
|
||||||
|
|
||||||
|
# utils
|
||||||
|
whitespace = just(" ").or_(just("\n")).repeat()
|
||||||
|
T = TypeVar("T")
|
||||||
|
def lexeme(p: Parser[T]) -> Parser[T]:
|
||||||
|
return p << whitespace
|
||||||
|
|
||||||
|
|
||||||
|
suite = Suite("cases")
|
||||||
|
|
||||||
|
|
||||||
|
@suite.test()
|
||||||
|
def case_json(ctx):
|
||||||
|
# Punctuation
|
||||||
|
lbrace = lexeme(just("{"))
|
||||||
|
rbrace = lexeme(just("}"))
|
||||||
|
lbrack = lexeme(just("["))
|
||||||
|
rbrack = lexeme(just("]"))
|
||||||
|
colon = lexeme(just(":"))
|
||||||
|
comma = lexeme(just(","))
|
||||||
|
# Primitives
|
||||||
|
true = lexeme(just("true")).set(True)
|
||||||
|
false = lexeme(just("false")).set(False)
|
||||||
|
null = lexeme(just("null")).set(None)
|
||||||
|
number = lexeme(regex(r"-?(0|[1-9][0-9]*)([.][0-9]+)?([eE][+-]?[0-9]+)?")).map(float)
|
||||||
|
just_part = regex(r'[^"\\]+')
|
||||||
|
just_esc = just("\\") >> ( just("\\") | just("/") | just('"') | just("b").set("\b") | just("f").set("\f") | just("n").set("\n") | just("r").set("\r") | just("t").set("\t") | regex(r"u[0-9a-fA-F]{4}").map(lambda s: chr(int(s[1:], 16))))
|
||||||
|
quoted = lexeme(just('"') >> (just_part | just_esc).repeat().map(lambda l: "".join(l)) << just('"'))
|
||||||
|
# Data structures
|
||||||
|
json = Declare[Any]()
|
||||||
|
object_pair = (quoted << colon) & json.parser()
|
||||||
|
json_object = lbrace >> object_pair.sep_by(comma).map(dict) << rbrace
|
||||||
|
array = lbrack >> json.parser().sep_by(comma) << rbrack
|
||||||
|
# Everything
|
||||||
|
json.define(quoted | number | json_object | array | true | false | null)
|
||||||
|
json = json.parser()
|
||||||
|
# asserts
|
||||||
|
assert json.parse("1") == 1
|
||||||
|
assert json.parse('"1"') == "1"
|
||||||
|
assert json.parse('["1", 1]') == ["1", 1]
|
||||||
|
assert json.parse('{"1": 1}') == {"1": 1}
|
||||||
|
assert json.parse(r"""{
|
||||||
|
"int": 1,
|
||||||
|
"just": "hello",
|
||||||
|
"a list": [1, 2, 3],
|
||||||
|
"escapes": "\n \u24D2",
|
||||||
|
"nested": {"x": "y"},
|
||||||
|
"other": [true, false, null]
|
||||||
|
}""") == {
|
||||||
|
"int": 1,
|
||||||
|
"just": "hello",
|
||||||
|
"a list": [1, 2, 3],
|
||||||
|
"escapes": "\n ⓒ",
|
||||||
|
"nested": {"x": "y"},
|
||||||
|
"other": [True, False, None],
|
||||||
|
}
|
||||||
|
|
||||||
|
if __name__ == "__main__": suite.run(sys.argv[1:])
|
12
tests/inline.py
Executable file
12
tests/inline.py
Executable file
|
@ -0,0 +1,12 @@
|
||||||
|
#!/bin/env -S python
|
||||||
|
|
||||||
|
from os.path import dirname
|
||||||
|
import sys
|
||||||
|
|
||||||
|
from okipy.lib import get_inline_suite
|
||||||
|
|
||||||
|
sys.path.append(f"{dirname(__file__)}/../src")
|
||||||
|
import pyalibert # executes inline tests
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__": get_inline_suite().run(sys.argv[1:])
|
|
@ -1,86 +0,0 @@
|
||||||
from os.path import dirname
|
|
||||||
import sys
|
|
||||||
|
|
||||||
sys.path.append(f"{dirname(__file__)}/../src")
|
|
||||||
from party import regex, Parser, just, FwDeclaration
|
|
||||||
|
|
||||||
from typing import TypeVar, Any
|
|
||||||
|
|
||||||
# Utilities
|
|
||||||
# whitespace = regex(r"\s*")
|
|
||||||
whitespace = just(" ").or_else(just("\n")).repeat()
|
|
||||||
|
|
||||||
T = TypeVar("T")
|
|
||||||
|
|
||||||
|
|
||||||
def lexeme(p: Parser[T]) -> Parser[T]:
|
|
||||||
return p << whitespace
|
|
||||||
|
|
||||||
|
|
||||||
# Punctuation
|
|
||||||
lbrace = lexeme(just("{"))
|
|
||||||
rbrace = lexeme(just("}"))
|
|
||||||
lbrack = lexeme(just("["))
|
|
||||||
rbrack = lexeme(just("]"))
|
|
||||||
colon = lexeme(just(":"))
|
|
||||||
comma = lexeme(just(","))
|
|
||||||
|
|
||||||
# Primitives
|
|
||||||
true = lexeme(just("true")).value(True)
|
|
||||||
false = lexeme(just("false")).value(False)
|
|
||||||
null = lexeme(just("null")).value(None)
|
|
||||||
number = lexeme(regex(r"-?(0|[1-9][0-9]*)([.][0-9]+)?([eE][+-]?[0-9]+)?")).map(float)
|
|
||||||
just_part = regex(r'[^"\\]+')
|
|
||||||
just_esc = just("\\") >> (
|
|
||||||
just("\\")
|
|
||||||
| just("/")
|
|
||||||
| just('"')
|
|
||||||
| just("b").value("\b")
|
|
||||||
| just("f").value("\f")
|
|
||||||
| just("n").value("\n")
|
|
||||||
| just("r").value("\r")
|
|
||||||
| just("t").value("\t")
|
|
||||||
| regex(r"u[0-9a-fA-F]{4}").map(lambda s: chr(int(s[1:], 16)))
|
|
||||||
)
|
|
||||||
quoted = lexeme(just('"') >> (just_part | just_esc).repeat().map(lambda l: "".join(l)) << just('"'))
|
|
||||||
|
|
||||||
# Data structures
|
|
||||||
json_value = FwDeclaration[Any]()
|
|
||||||
object_pair = (quoted << colon) & json_value.p()
|
|
||||||
json_object = lbrace >> object_pair.sep_by(comma).map(dict) << rbrace
|
|
||||||
array = lbrack >> json_value.p().sep_by(comma) << rbrack
|
|
||||||
|
|
||||||
# Everything
|
|
||||||
json_value.define(quoted | number | json_object | array | true | false | null)
|
|
||||||
json_doc = whitespace >> json_value.p()
|
|
||||||
|
|
||||||
|
|
||||||
def test():
|
|
||||||
assert (
|
|
||||||
json_doc.parse(
|
|
||||||
r"""
|
|
||||||
{
|
|
||||||
"int": 1,
|
|
||||||
"just": "hello",
|
|
||||||
"a list": [1, 2, 3],
|
|
||||||
"escapes": "\n \u24D2",
|
|
||||||
"nested": {"x": "y"},
|
|
||||||
"other": [true, false, null]
|
|
||||||
}
|
|
||||||
"""
|
|
||||||
)
|
|
||||||
== {
|
|
||||||
"int": 1,
|
|
||||||
"just": "hello",
|
|
||||||
"a list": [1, 2, 3],
|
|
||||||
"escapes": "\n ⓒ",
|
|
||||||
"nested": {"x": "y"},
|
|
||||||
"other": [True, False, None],
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
from sys import stdin
|
|
||||||
test()
|
|
||||||
# print(repr(json_doc.parse(stdin.read())))
|
|
|
@ -1,61 +0,0 @@
|
||||||
#!/bin/env -S python
|
|
||||||
|
|
||||||
from os.path import dirname
|
|
||||||
import sys
|
|
||||||
|
|
||||||
sys.path.append(f"{dirname(__file__)}/../src")
|
|
||||||
from party import regex, Parser, just, FwDeclaration, end
|
|
||||||
|
|
||||||
|
|
||||||
# input = "abc"
|
|
||||||
# parser = just("a").or_else(just("b")).or_else(just("c"))
|
|
||||||
# (parsed1, rest1) = parser.parse_part(input)
|
|
||||||
# (parsed2, rest2) = parser.parse_part(rest1)
|
|
||||||
# (parsed3, rest3) = parser.parse_part(rest2)
|
|
||||||
|
|
||||||
# print(
|
|
||||||
# "parsed1", parsed1,
|
|
||||||
# "rest1", rest1,
|
|
||||||
# "parsed2", parsed2,
|
|
||||||
# "rest2", rest2,
|
|
||||||
# "parsed3", parsed3,
|
|
||||||
# "rest3", rest3,
|
|
||||||
# )
|
|
||||||
|
|
||||||
|
|
||||||
# input = "a"
|
|
||||||
# parser = just("a").and_then(end())
|
|
||||||
# parsed = parser.parse(input)
|
|
||||||
|
|
||||||
# print("parsed", parsed)
|
|
||||||
|
|
||||||
|
|
||||||
# input = "...a"
|
|
||||||
# parser = just(".").repeat() >> just("a")
|
|
||||||
# parsed = parser.parse(input)
|
|
||||||
|
|
||||||
# print("parsed", parsed)
|
|
||||||
|
|
||||||
|
|
||||||
# input = r"{a}"
|
|
||||||
# parser = just("{") >> just("a") << just("}")
|
|
||||||
# parsed = parser.parse(input)
|
|
||||||
|
|
||||||
# print("parsed", parsed)
|
|
||||||
|
|
||||||
|
|
||||||
# input = r"a,a,a,a"
|
|
||||||
# parser = just("a").sep_by(just(","))
|
|
||||||
# parsed = parser.parse(input)
|
|
||||||
|
|
||||||
# print("parsed", parsed)
|
|
||||||
|
|
||||||
|
|
||||||
#td
|
|
||||||
input = r"{a,a,a,a"
|
|
||||||
# parser = just("{") >> just("a").sep_by(just(","))
|
|
||||||
# parser = just("{").and_then(just("a").sep_by(just(",")))
|
|
||||||
parser = just("{").and_then(just("a").sep_by(just(",")))
|
|
||||||
parsed = parser.parse(input)
|
|
||||||
|
|
||||||
print("parsed", parsed)
|
|
7
tests/test.sh
Executable file
7
tests/test.sh
Executable file
|
@ -0,0 +1,7 @@
|
||||||
|
#!/bin/sh
|
||||||
|
set -e
|
||||||
|
cd "$(dirname "$(realpath "$0")")"
|
||||||
|
|
||||||
|
source ../venv/bin/activate
|
||||||
|
./inline.py
|
||||||
|
./cases.py
|
Loading…
Add table
Add a link
Reference in a new issue