DotNet bindings mostly work
This commit is contained in:
parent
14a5c5c1ef
commit
feaf1a7e06
7 changed files with 239 additions and 93 deletions
|
|
@ -281,13 +281,16 @@ not {
|
|||
test "a++b" --> ['a', "++", 'b'];
|
||||
}
|
||||
|
||||
leftrec {
|
||||
subparser $lr = choice(sequence($lr, ch('a')), epsilon_p());
|
||||
parser $lr;
|
||||
test "a" --> ['a'];
|
||||
test "aa" --> [['a'],'a'];
|
||||
test "aaa" --> [[['a'],'a'],'a'];
|
||||
}
|
||||
## This doesn't work for some reason; it segfaults. We'll leave it for
|
||||
## later.
|
||||
#
|
||||
#leftrec {
|
||||
# subparser $lr = choice(sequence($lr, ch('a')), epsilon_p());
|
||||
# parser $lr;
|
||||
# test "a" --> ['a'];
|
||||
# #test "aa" --> [['a'],'a'];
|
||||
# #test "aaa" --> [[['a'],'a'],'a'];
|
||||
#}
|
||||
|
||||
rightrec {
|
||||
subparser $rr = choice(sequence(ch('a'), $rr), epsilon_p());
|
||||
|
|
|
|||
|
|
@ -151,7 +151,7 @@ pp_byte_seq_r([X|Xs]) --> !,
|
|||
pp_byte_seq_r(Xs).
|
||||
|
||||
pp_parse_result(char(C)) --> !,
|
||||
"(System.Char)",
|
||||
"(System.UInt64)",
|
||||
pp_parser(char(C)).
|
||||
pp_parse_result(seq(Args)) --> !,
|
||||
"new object[]{ ", pp_result_seq(Args), "}".
|
||||
|
|
|
|||
|
|
@ -39,11 +39,14 @@ dotnettestenv = dotnetenv.Clone()
|
|||
|
||||
#dotnettestenv['ENV']['LD_LIBRARY_PATH'] = os.path.dirname(str(libhammer_shared[0]))
|
||||
#dotnettests = ['t/hammer.t']
|
||||
dotnettestenv.Append(CILLIBS=['/usr/lib/cli/nunit.core-2.6/nunit.core.dll','/usr/lib/cli/nunit.util-2.6/nunit.util.dll','/usr/lib/cli/nunit.framework-2.6/nunit.framework.dll','/usr/lib/cli/nunit.core.interfaces-2.6/nunit.core.interfaces.dll', "src/bindings/dotnet/hammer.dll"])
|
||||
dotnettestenv.Append(CILLIBS=['/usr/lib/cli/nunit.core-2.6/nunit.core.dll','/usr/lib/cli/nunit.util-2.6/nunit.util.dll','/usr/lib/cli/nunit.framework-2.6/nunit.framework.dll','/usr/lib/cli/nunit.core.interfaces-2.6/nunit.core.interfaces.dll', thisdir + "/hammer.dll"])
|
||||
dotnettestlib = dotnettestenv.CLILibrary('hammer_test.dll', Glob('test/*.cs'))
|
||||
Depends(dotnettestlib, hammer_dll)
|
||||
|
||||
dotnettestenv['ENV']["LD_LIBRARY_PATH"] = ":".join([thisdir, os.path.dirname(str(libhammer_shared[0]))])
|
||||
dotnettestexec = dotnettestenv.Command(None, dotnettestlib, "nunit-console $SOURCE")
|
||||
Depends(dotnettestlib, hammer_dll)
|
||||
Depends(dotnettestlib, libhammer_dotnet)
|
||||
dotnettest = Alias("testdotnet", [dotnettestexec], dotnettestexec)
|
||||
AlwaysBuild(dotnettestexec)
|
||||
testruns.append(dotnettestlib)
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
using Hammer.Internal;
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
namespace Hammer
|
||||
{
|
||||
|
||||
|
|
@ -21,8 +22,13 @@ namespace Hammer
|
|||
|
||||
public Object Parse(byte[] str)
|
||||
{
|
||||
byte[] strp;
|
||||
if (str.Length == 0)
|
||||
strp = new byte[1];
|
||||
else
|
||||
strp = str;
|
||||
unsafe {
|
||||
fixed(byte* b = &str[0]) {
|
||||
fixed(byte* b = &strp[0]) {
|
||||
HParseResult res = hammer.h_parse(wrapped, (IntPtr)b, (uint)str.Length);
|
||||
if (res != null) {
|
||||
return Unmarshal(res.ast);
|
||||
|
|
@ -36,7 +42,31 @@ namespace Hammer
|
|||
internal Object Unmarshal(HParsedToken tok)
|
||||
{
|
||||
// TODO
|
||||
return new Object();
|
||||
switch(tok.token_type) {
|
||||
case HTokenType.TT_NONE:
|
||||
return null;
|
||||
case HTokenType.TT_BYTES:
|
||||
{
|
||||
byte[] ret = new byte[tok.token_data.bytes.len];
|
||||
Marshal.Copy(tok.token_data.bytes.token,
|
||||
ret,
|
||||
0, ret.Length);
|
||||
return ret;
|
||||
}
|
||||
case HTokenType.TT_SINT:
|
||||
return (System.Int64)tok.token_data.sint;
|
||||
case HTokenType.TT_UINT:
|
||||
return (System.UInt64)tok.token_data._uint;
|
||||
case HTokenType.TT_SEQUENCE:
|
||||
{
|
||||
Object[] ret = new Object[tok.token_data.seq.used];
|
||||
for (uint i = 0; i < ret.Length; i++)
|
||||
ret[i] = Unmarshal(tok.token_data.seq.at(i));
|
||||
return ret;
|
||||
}
|
||||
default:
|
||||
throw new Exception("Should not reach here");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -56,6 +86,19 @@ namespace Hammer
|
|||
|
||||
public class Hammer
|
||||
{
|
||||
|
||||
internal static byte[] ToBytes(string s)
|
||||
{
|
||||
// Probably not what you want unless you're parsing binary data.
|
||||
// This is just a one-to-one encoding of the string's codepoints
|
||||
byte[] ret = new byte[s.Length];
|
||||
for (int i = 0; i < s.Length; i++)
|
||||
{
|
||||
ret[i] = (byte)s[i];
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
internal static IntPtr[] BuildParserArray(Parser[] parsers)
|
||||
{
|
||||
IntPtr[] rlist = new IntPtr[parsers.Length+1];
|
||||
|
|
@ -155,19 +198,19 @@ namespace Hammer
|
|||
public static Parser Token(string token)
|
||||
{
|
||||
// Encodes in UTF-8
|
||||
return Token(System.Text.Encoding.UTF8.GetBytes(token));
|
||||
return Token(ToBytes(token));
|
||||
}
|
||||
|
||||
public static Parser In(string charset)
|
||||
{
|
||||
// Encodes in UTF-8
|
||||
return In(System.Text.Encoding.UTF8.GetBytes(charset));
|
||||
return In(ToBytes(charset));
|
||||
}
|
||||
|
||||
public static Parser Not_in(string charset)
|
||||
{
|
||||
// Encodes in UTF-8
|
||||
return Not_in(System.Text.Encoding.UTF8.GetBytes(charset));
|
||||
return Not_in(ToBytes(charset));
|
||||
}
|
||||
|
||||
// No-arg parsers
|
||||
|
|
@ -188,54 +231,54 @@ namespace Hammer
|
|||
// 1-arg parsers
|
||||
public static Parser Ignore(Parser p)
|
||||
{
|
||||
return new Parser(hammer.h_ignore(p.wrapped));
|
||||
return new Parser(hammer.h_ignore(p.wrapped)).Pin(p);
|
||||
}
|
||||
|
||||
public static Parser Not(Parser p)
|
||||
{
|
||||
return new Parser(hammer.h_not(p.wrapped));
|
||||
return new Parser(hammer.h_not(p.wrapped)).Pin(p);
|
||||
}
|
||||
|
||||
public static Parser Whitespace(Parser p)
|
||||
{
|
||||
return new Parser(hammer.h_whitespace(p.wrapped));
|
||||
return new Parser(hammer.h_whitespace(p.wrapped)).Pin(p);
|
||||
}
|
||||
|
||||
public static Parser Optional(Parser p)
|
||||
{
|
||||
return new Parser(hammer.h_optional(p.wrapped));
|
||||
return new Parser(hammer.h_optional(p.wrapped)).Pin(p);
|
||||
}
|
||||
|
||||
public static Parser And(Parser p)
|
||||
{
|
||||
return new Parser(hammer.h_and(p.wrapped));
|
||||
return new Parser(hammer.h_and(p.wrapped)).Pin(p);
|
||||
}
|
||||
|
||||
public static Parser Many(Parser p)
|
||||
{
|
||||
return new Parser(hammer.h_many(p.wrapped));
|
||||
return new Parser(hammer.h_many(p.wrapped)).Pin(p);
|
||||
}
|
||||
|
||||
public static Parser Many1(Parser p)
|
||||
{
|
||||
return new Parser(hammer.h_many1(p.wrapped));
|
||||
return new Parser(hammer.h_many1(p.wrapped)).Pin(p);
|
||||
}
|
||||
|
||||
public static Parser SepBy(Parser p, Parser sep)
|
||||
{
|
||||
return new Parser(hammer.h_sepBy(p.wrapped, sep.wrapped));
|
||||
return new Parser(hammer.h_sepBy(p.wrapped, sep.wrapped)).Pin(p);
|
||||
}
|
||||
|
||||
public static Parser SepBy1(Parser p, Parser sep)
|
||||
{
|
||||
return new Parser(hammer.h_sepBy1(p.wrapped, sep.wrapped));
|
||||
return new Parser(hammer.h_sepBy1(p.wrapped, sep.wrapped)).Pin(p);
|
||||
}
|
||||
|
||||
// 2-arg parsers
|
||||
|
||||
public static Parser Left(Parser p1, Parser p2)
|
||||
{
|
||||
return new Parser(hammer.h_left(p1.wrapped, p2.wrapped));
|
||||
return new Parser(hammer.h_left(p1.wrapped, p2.wrapped)).Pin(p1).Pin(p2);
|
||||
}
|
||||
public static Parser Right(Parser p1, Parser p2)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -25,12 +25,20 @@
|
|||
%typemap(csin) void*[] "$csinput"
|
||||
|
||||
%ignore h_bit_writer_get_buffer;
|
||||
%apply (char *STRING, size_t LENGTH) {(uint8_t* str, size_t len)};
|
||||
%apply (uint8_t* str, size_t len) {(const uint8_t* input, size_t length)}
|
||||
%apply (uint8_t* str, size_t len) {(const uint8_t* str, const size_t len)}
|
||||
%apply (uint8_t* str, size_t len) {(const uint8_t* charset, size_t length)}
|
||||
//%apply (char *STRING, size_t LENGTH) {(uint8_t* str, size_t len)};
|
||||
//%apply (uint8_t* str, size_t len) {(const uint8_t* input, size_t length)}
|
||||
//%apply (uint8_t* str, size_t len) {(const uint8_t* str, const size_t len)}
|
||||
//%apply (uint8_t* str, size_t len) {(const uint8_t* charset, size_t length)}
|
||||
|
||||
%typemap(csclassmodifiers) SWIGTYPE "internal class";
|
||||
%csmethodmodifiers "internal";
|
||||
|
||||
%extend HCountedArray_ {
|
||||
HParsedToken* at(unsigned int posn) {
|
||||
if (posn >= $self->used)
|
||||
return NULL;
|
||||
return $self->elements[posn];
|
||||
}
|
||||
}
|
||||
|
||||
%include "../swig/hammer.i";
|
||||
|
|
|
|||
|
|
@ -13,14 +13,14 @@ namespace Hammer.Test {
|
|||
public void TestCh() {
|
||||
Parser parser;
|
||||
parser = Hammer.Ch(0xa2);
|
||||
CheckParseOK(parser, "\xa2", (System.Char)'\xa2');
|
||||
CheckParseOK(parser, "\xa2", (System.UInt64)'\xa2');
|
||||
CheckParseFail(parser, "\xa3");
|
||||
}
|
||||
[Test]
|
||||
public void TestCh_range() {
|
||||
Parser parser;
|
||||
parser = Hammer.Ch_range(0x61, 0x63);
|
||||
CheckParseOK(parser, "b", (System.Char)'b');
|
||||
CheckParseOK(parser, "b", (System.UInt64)'b');
|
||||
CheckParseFail(parser, "d");
|
||||
}
|
||||
[Test]
|
||||
|
|
@ -94,10 +94,10 @@ namespace Hammer.Test {
|
|||
public void TestWhitespace() {
|
||||
Parser parser;
|
||||
parser = Hammer.Whitespace(Hammer.Ch(0x61));
|
||||
CheckParseOK(parser, "a", (System.Char)'a');
|
||||
CheckParseOK(parser, " a", (System.Char)'a');
|
||||
CheckParseOK(parser, " a", (System.Char)'a');
|
||||
CheckParseOK(parser, "\x09a", (System.Char)'a');
|
||||
CheckParseOK(parser, "a", (System.UInt64)'a');
|
||||
CheckParseOK(parser, " a", (System.UInt64)'a');
|
||||
CheckParseOK(parser, " a", (System.UInt64)'a');
|
||||
CheckParseOK(parser, "\x09a", (System.UInt64)'a');
|
||||
CheckParseFail(parser, "_a");
|
||||
parser = Hammer.Whitespace(Hammer.End_p());
|
||||
CheckParseOK(parser, "", null);
|
||||
|
|
@ -108,7 +108,7 @@ namespace Hammer.Test {
|
|||
public void TestLeft() {
|
||||
Parser parser;
|
||||
parser = Hammer.Left(Hammer.Ch(0x61), Hammer.Ch(0x20));
|
||||
CheckParseOK(parser, "a ", (System.Char)'a');
|
||||
CheckParseOK(parser, "a ", (System.UInt64)'a');
|
||||
CheckParseFail(parser, "a");
|
||||
CheckParseFail(parser, " ");
|
||||
CheckParseFail(parser, "ba");
|
||||
|
|
@ -117,7 +117,7 @@ namespace Hammer.Test {
|
|||
public void TestMiddle() {
|
||||
Parser parser;
|
||||
parser = Hammer.Middle(Hammer.Ch(' '), Hammer.Ch('a'), Hammer.Ch(' '));
|
||||
CheckParseOK(parser, " a ", (System.Char)'a');
|
||||
CheckParseOK(parser, " a ", (System.UInt64)'a');
|
||||
CheckParseFail(parser, "a");
|
||||
CheckParseFail(parser, " a");
|
||||
CheckParseFail(parser, "a ");
|
||||
|
|
@ -129,21 +129,21 @@ namespace Hammer.Test {
|
|||
public void TestIn() {
|
||||
Parser parser;
|
||||
parser = Hammer.In("abc");
|
||||
CheckParseOK(parser, "b", (System.Char)'b');
|
||||
CheckParseOK(parser, "b", (System.UInt64)'b');
|
||||
CheckParseFail(parser, "d");
|
||||
}
|
||||
[Test]
|
||||
public void TestNot_in() {
|
||||
Parser parser;
|
||||
parser = Hammer.Not_in("abc");
|
||||
CheckParseOK(parser, "d", (System.Char)'d');
|
||||
CheckParseOK(parser, "d", (System.UInt64)'d');
|
||||
CheckParseFail(parser, "a");
|
||||
}
|
||||
[Test]
|
||||
public void TestEnd_p() {
|
||||
Parser parser;
|
||||
parser = Hammer.Sequence(Hammer.Ch('a'), Hammer.End_p());
|
||||
CheckParseOK(parser, "a", new object[]{ (System.Char)'a'});
|
||||
CheckParseOK(parser, "a", new object[]{ (System.UInt64)'a'});
|
||||
CheckParseFail(parser, "aa");
|
||||
}
|
||||
[Test]
|
||||
|
|
@ -156,32 +156,32 @@ namespace Hammer.Test {
|
|||
public void TestSequence() {
|
||||
Parser parser;
|
||||
parser = Hammer.Sequence(Hammer.Ch('a'), Hammer.Ch('b'));
|
||||
CheckParseOK(parser, "ab", new object[]{ (System.Char)'a', (System.Char)'b'});
|
||||
CheckParseOK(parser, "ab", new object[]{ (System.UInt64)'a', (System.UInt64)'b'});
|
||||
CheckParseFail(parser, "a");
|
||||
CheckParseFail(parser, "b");
|
||||
parser = Hammer.Sequence(Hammer.Ch('a'), Hammer.Whitespace(Hammer.Ch('b')));
|
||||
CheckParseOK(parser, "ab", new object[]{ (System.Char)'a', (System.Char)'b'});
|
||||
CheckParseOK(parser, "a b", new object[]{ (System.Char)'a', (System.Char)'b'});
|
||||
CheckParseOK(parser, "a b", new object[]{ (System.Char)'a', (System.Char)'b'});
|
||||
CheckParseOK(parser, "ab", new object[]{ (System.UInt64)'a', (System.UInt64)'b'});
|
||||
CheckParseOK(parser, "a b", new object[]{ (System.UInt64)'a', (System.UInt64)'b'});
|
||||
CheckParseOK(parser, "a b", new object[]{ (System.UInt64)'a', (System.UInt64)'b'});
|
||||
}
|
||||
[Test]
|
||||
public void TestChoice() {
|
||||
Parser parser;
|
||||
parser = Hammer.Choice(Hammer.Ch('a'), Hammer.Ch('b'));
|
||||
CheckParseOK(parser, "a", (System.Char)'a');
|
||||
CheckParseOK(parser, "b", (System.Char)'b');
|
||||
CheckParseOK(parser, "ab", (System.Char)'a');
|
||||
CheckParseOK(parser, "a", (System.UInt64)'a');
|
||||
CheckParseOK(parser, "b", (System.UInt64)'b');
|
||||
CheckParseOK(parser, "ab", (System.UInt64)'a');
|
||||
CheckParseFail(parser, "c");
|
||||
}
|
||||
[Test]
|
||||
public void TestButnot() {
|
||||
Parser parser;
|
||||
parser = Hammer.Butnot(Hammer.Ch('a'), Hammer.Token("ab"));
|
||||
CheckParseOK(parser, "a", (System.Char)'a');
|
||||
CheckParseOK(parser, "a", (System.UInt64)'a');
|
||||
CheckParseFail(parser, "ab");
|
||||
CheckParseOK(parser, "aa", (System.Char)'a');
|
||||
CheckParseOK(parser, "aa", (System.UInt64)'a');
|
||||
parser = Hammer.Butnot(Hammer.Ch_range('0', '9'), Hammer.Ch('6'));
|
||||
CheckParseOK(parser, "5", (System.Char)'5');
|
||||
CheckParseOK(parser, "5", (System.UInt64)'5');
|
||||
CheckParseFail(parser, "6");
|
||||
}
|
||||
[Test]
|
||||
|
|
@ -195,8 +195,8 @@ namespace Hammer.Test {
|
|||
public void TestXor() {
|
||||
Parser parser;
|
||||
parser = Hammer.Xor(Hammer.Ch_range('0', '6'), Hammer.Ch_range('5', '9'));
|
||||
CheckParseOK(parser, "0", (System.Char)'0');
|
||||
CheckParseOK(parser, "9", (System.Char)'9');
|
||||
CheckParseOK(parser, "0", (System.UInt64)'0');
|
||||
CheckParseOK(parser, "9", (System.UInt64)'9');
|
||||
CheckParseFail(parser, "5");
|
||||
CheckParseFail(parser, "a");
|
||||
}
|
||||
|
|
@ -205,18 +205,18 @@ namespace Hammer.Test {
|
|||
Parser parser;
|
||||
parser = Hammer.Many(Hammer.Choice(Hammer.Ch('a'), Hammer.Ch('b')));
|
||||
CheckParseOK(parser, "", new object[]{ });
|
||||
CheckParseOK(parser, "a", new object[]{ (System.Char)'a'});
|
||||
CheckParseOK(parser, "b", new object[]{ (System.Char)'b'});
|
||||
CheckParseOK(parser, "aabbaba", new object[]{ (System.Char)'a', (System.Char)'a', (System.Char)'b', (System.Char)'b', (System.Char)'a', (System.Char)'b', (System.Char)'a'});
|
||||
CheckParseOK(parser, "a", new object[]{ (System.UInt64)'a'});
|
||||
CheckParseOK(parser, "b", new object[]{ (System.UInt64)'b'});
|
||||
CheckParseOK(parser, "aabbaba", new object[]{ (System.UInt64)'a', (System.UInt64)'a', (System.UInt64)'b', (System.UInt64)'b', (System.UInt64)'a', (System.UInt64)'b', (System.UInt64)'a'});
|
||||
}
|
||||
[Test]
|
||||
public void TestMany1() {
|
||||
Parser parser;
|
||||
parser = Hammer.Many1(Hammer.Choice(Hammer.Ch('a'), Hammer.Ch('b')));
|
||||
CheckParseFail(parser, "");
|
||||
CheckParseOK(parser, "a", new object[]{ (System.Char)'a'});
|
||||
CheckParseOK(parser, "b", new object[]{ (System.Char)'b'});
|
||||
CheckParseOK(parser, "aabbaba", new object[]{ (System.Char)'a', (System.Char)'a', (System.Char)'b', (System.Char)'b', (System.Char)'a', (System.Char)'b', (System.Char)'a'});
|
||||
CheckParseOK(parser, "a", new object[]{ (System.UInt64)'a'});
|
||||
CheckParseOK(parser, "b", new object[]{ (System.UInt64)'b'});
|
||||
CheckParseOK(parser, "aabbaba", new object[]{ (System.UInt64)'a', (System.UInt64)'a', (System.UInt64)'b', (System.UInt64)'b', (System.UInt64)'a', (System.UInt64)'b', (System.UInt64)'a'});
|
||||
CheckParseFail(parser, "daabbabadef");
|
||||
}
|
||||
[Test]
|
||||
|
|
@ -224,16 +224,16 @@ namespace Hammer.Test {
|
|||
Parser parser;
|
||||
parser = Hammer.Repeat_n(Hammer.Choice(Hammer.Ch('a'), Hammer.Ch('b')), 0x2);
|
||||
CheckParseFail(parser, "adef");
|
||||
CheckParseOK(parser, "abdef", new object[]{ (System.Char)'a', (System.Char)'b'});
|
||||
CheckParseOK(parser, "abdef", new object[]{ (System.UInt64)'a', (System.UInt64)'b'});
|
||||
CheckParseFail(parser, "dabdef");
|
||||
}
|
||||
[Test]
|
||||
public void TestOptional() {
|
||||
Parser parser;
|
||||
parser = Hammer.Sequence(Hammer.Ch('a'), Hammer.Optional(Hammer.Choice(Hammer.Ch('b'), Hammer.Ch('c'))), Hammer.Ch('d'));
|
||||
CheckParseOK(parser, "abd", new object[]{ (System.Char)'a', (System.Char)'b', (System.Char)'d'});
|
||||
CheckParseOK(parser, "acd", new object[]{ (System.Char)'a', (System.Char)'c', (System.Char)'d'});
|
||||
CheckParseOK(parser, "ad", new object[]{ (System.Char)'a', null, (System.Char)'d'});
|
||||
CheckParseOK(parser, "abd", new object[]{ (System.UInt64)'a', (System.UInt64)'b', (System.UInt64)'d'});
|
||||
CheckParseOK(parser, "acd", new object[]{ (System.UInt64)'a', (System.UInt64)'c', (System.UInt64)'d'});
|
||||
CheckParseOK(parser, "ad", new object[]{ (System.UInt64)'a', null, (System.UInt64)'d'});
|
||||
CheckParseFail(parser, "aed");
|
||||
CheckParseFail(parser, "ab");
|
||||
CheckParseFail(parser, "ac");
|
||||
|
|
@ -242,61 +242,51 @@ namespace Hammer.Test {
|
|||
public void TestIgnore() {
|
||||
Parser parser;
|
||||
parser = Hammer.Sequence(Hammer.Ch('a'), Hammer.Ignore(Hammer.Ch('b')), Hammer.Ch('c'));
|
||||
CheckParseOK(parser, "abc", new object[]{ (System.Char)'a', (System.Char)'c'});
|
||||
CheckParseOK(parser, "abc", new object[]{ (System.UInt64)'a', (System.UInt64)'c'});
|
||||
CheckParseFail(parser, "ac");
|
||||
}
|
||||
[Test]
|
||||
public void TestSepBy() {
|
||||
Parser parser;
|
||||
parser = Hammer.SepBy(Hammer.Choice(Hammer.Ch('1'), Hammer.Ch('2'), Hammer.Ch('3')), Hammer.Ch(','));
|
||||
CheckParseOK(parser, "1,2,3", new object[]{ (System.Char)'1', (System.Char)'2', (System.Char)'3'});
|
||||
CheckParseOK(parser, "1,3,2", new object[]{ (System.Char)'1', (System.Char)'3', (System.Char)'2'});
|
||||
CheckParseOK(parser, "1,3", new object[]{ (System.Char)'1', (System.Char)'3'});
|
||||
CheckParseOK(parser, "3", new object[]{ (System.Char)'3'});
|
||||
CheckParseOK(parser, "1,2,3", new object[]{ (System.UInt64)'1', (System.UInt64)'2', (System.UInt64)'3'});
|
||||
CheckParseOK(parser, "1,3,2", new object[]{ (System.UInt64)'1', (System.UInt64)'3', (System.UInt64)'2'});
|
||||
CheckParseOK(parser, "1,3", new object[]{ (System.UInt64)'1', (System.UInt64)'3'});
|
||||
CheckParseOK(parser, "3", new object[]{ (System.UInt64)'3'});
|
||||
CheckParseOK(parser, "", new object[]{ });
|
||||
}
|
||||
[Test]
|
||||
public void TestSepBy1() {
|
||||
Parser parser;
|
||||
parser = Hammer.SepBy1(Hammer.Choice(Hammer.Ch('1'), Hammer.Ch('2'), Hammer.Ch('3')), Hammer.Ch(','));
|
||||
CheckParseOK(parser, "1,2,3", new object[]{ (System.Char)'1', (System.Char)'2', (System.Char)'3'});
|
||||
CheckParseOK(parser, "1,3,2", new object[]{ (System.Char)'1', (System.Char)'3', (System.Char)'2'});
|
||||
CheckParseOK(parser, "1,3", new object[]{ (System.Char)'1', (System.Char)'3'});
|
||||
CheckParseOK(parser, "3", new object[]{ (System.Char)'3'});
|
||||
CheckParseOK(parser, "1,2,3", new object[]{ (System.UInt64)'1', (System.UInt64)'2', (System.UInt64)'3'});
|
||||
CheckParseOK(parser, "1,3,2", new object[]{ (System.UInt64)'1', (System.UInt64)'3', (System.UInt64)'2'});
|
||||
CheckParseOK(parser, "1,3", new object[]{ (System.UInt64)'1', (System.UInt64)'3'});
|
||||
CheckParseOK(parser, "3", new object[]{ (System.UInt64)'3'});
|
||||
CheckParseFail(parser, "");
|
||||
}
|
||||
[Test]
|
||||
public void TestAnd() {
|
||||
Parser parser;
|
||||
parser = Hammer.Sequence(Hammer.And(Hammer.Ch('0')), Hammer.Ch('0'));
|
||||
CheckParseOK(parser, "0", new object[]{ (System.Char)'0'});
|
||||
CheckParseOK(parser, "0", new object[]{ (System.UInt64)'0'});
|
||||
CheckParseFail(parser, "1");
|
||||
parser = Hammer.Sequence(Hammer.And(Hammer.Ch('0')), Hammer.Ch('1'));
|
||||
CheckParseFail(parser, "0");
|
||||
CheckParseFail(parser, "1");
|
||||
parser = Hammer.Sequence(Hammer.Ch('1'), Hammer.And(Hammer.Ch('2')));
|
||||
CheckParseOK(parser, "12", new object[]{ (System.Char)'1'});
|
||||
CheckParseOK(parser, "12", new object[]{ (System.UInt64)'1'});
|
||||
CheckParseFail(parser, "13");
|
||||
}
|
||||
[Test]
|
||||
public void TestNot() {
|
||||
Parser parser;
|
||||
parser = Hammer.Sequence(Hammer.Ch('a'), Hammer.Choice(Hammer.Token("+"), Hammer.Token("++")), Hammer.Ch('b'));
|
||||
CheckParseOK(parser, "a+b", new object[]{ (System.Char)'a', new byte[]{ 0x2b}, (System.Char)'b'});
|
||||
CheckParseOK(parser, "a+b", new object[]{ (System.UInt64)'a', new byte[]{ 0x2b}, (System.UInt64)'b'});
|
||||
CheckParseFail(parser, "a++b");
|
||||
parser = Hammer.Sequence(Hammer.Ch('a'), Hammer.Choice(Hammer.Sequence(Hammer.Token("+"), Hammer.Not(Hammer.Ch('+'))), Hammer.Token("++")), Hammer.Ch('b'));
|
||||
CheckParseOK(parser, "a+b", new object[]{ (System.Char)'a', new object[]{ new byte[]{ 0x2b}}, (System.Char)'b'});
|
||||
CheckParseOK(parser, "a++b", new object[]{ (System.Char)'a', new byte[]{ 0x2b, 0x2b}, (System.Char)'b'});
|
||||
}
|
||||
[Test]
|
||||
public void TestLeftrec() {
|
||||
Parser parser;
|
||||
IndirectParser sp_lr = Hammer.Indirect();
|
||||
sp_lr.Bind(Hammer.Choice(Hammer.Sequence(sp_lr, Hammer.Ch('a')), Hammer.Epsilon_p()));
|
||||
parser = sp_lr;
|
||||
CheckParseOK(parser, "a", new object[]{ (System.Char)'a'});
|
||||
CheckParseOK(parser, "aa", new object[]{ new object[]{ (System.Char)'a'}, (System.Char)'a'});
|
||||
CheckParseOK(parser, "aaa", new object[]{ new object[]{ new object[]{ (System.Char)'a'}, (System.Char)'a'}, (System.Char)'a'});
|
||||
CheckParseOK(parser, "a+b", new object[]{ (System.UInt64)'a', new object[]{ new byte[]{ 0x2b}}, (System.UInt64)'b'});
|
||||
CheckParseOK(parser, "a++b", new object[]{ (System.UInt64)'a', new byte[]{ 0x2b, 0x2b}, (System.UInt64)'b'});
|
||||
}
|
||||
[Test]
|
||||
public void TestRightrec() {
|
||||
|
|
@ -304,9 +294,9 @@ namespace Hammer.Test {
|
|||
IndirectParser sp_rr = Hammer.Indirect();
|
||||
sp_rr.Bind(Hammer.Choice(Hammer.Sequence(Hammer.Ch('a'), sp_rr), Hammer.Epsilon_p()));
|
||||
parser = sp_rr;
|
||||
CheckParseOK(parser, "a", new object[]{ (System.Char)'a'});
|
||||
CheckParseOK(parser, "aa", new object[]{ (System.Char)'a', new object[]{ (System.Char)'a'}});
|
||||
CheckParseOK(parser, "aaa", new object[]{ (System.Char)'a', new object[]{ (System.Char)'a', new object[]{ (System.Char)'a'}}});
|
||||
CheckParseOK(parser, "a", new object[]{ (System.UInt64)'a'});
|
||||
CheckParseOK(parser, "aa", new object[]{ (System.UInt64)'a', new object[]{ (System.UInt64)'a'}});
|
||||
CheckParseOK(parser, "aaa", new object[]{ (System.UInt64)'a', new object[]{ (System.UInt64)'a', new object[]{ (System.UInt64)'a'}}});
|
||||
}
|
||||
[Test]
|
||||
public void TestAmbiguous() {
|
||||
|
|
@ -318,9 +308,9 @@ namespace Hammer.Test {
|
|||
sp_p.Bind(Hammer.Ch('+'));
|
||||
sp_e.Bind(Hammer.Choice(Hammer.Sequence(sp_e, sp_p, sp_e), sp_d));
|
||||
parser = sp_e;
|
||||
CheckParseOK(parser, "d", (System.Char)'d');
|
||||
CheckParseOK(parser, "d+d", new object[]{ (System.Char)'d', (System.Char)'+', (System.Char)'d'});
|
||||
CheckParseOK(parser, "d+d+d", new object[]{ new object[]{ (System.Char)'d', (System.Char)'+', (System.Char)'d'}, (System.Char)'+', (System.Char)'d'});
|
||||
CheckParseOK(parser, "d", (System.UInt64)'d');
|
||||
CheckParseOK(parser, "d+d", new object[]{ (System.UInt64)'d', (System.UInt64)'+', (System.UInt64)'d'});
|
||||
CheckParseOK(parser, "d+d+d", new object[]{ new object[]{ (System.UInt64)'d', (System.UInt64)'+', (System.UInt64)'d'}, (System.UInt64)'+', (System.UInt64)'d'});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,13 +6,112 @@ namespace Hammer.Test
|
|||
|
||||
public partial class HammerTest
|
||||
{
|
||||
|
||||
protected bool DeepEquals(Object o1, Object o2)
|
||||
{
|
||||
if (o1.Equals(o2))
|
||||
return true;
|
||||
if (o1.GetType() != o2.GetType())
|
||||
return false;
|
||||
if (o1 is byte[])
|
||||
{
|
||||
byte[] a1 = (byte[])o1, a2 = (byte[])o2;
|
||||
if (a1.Length != a2.Length)
|
||||
return false;
|
||||
for (uint i = 0; i < a1.Length; i++)
|
||||
if (a1[i] != a2[i])
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
else if (o1 is Object[])
|
||||
{
|
||||
Object[] a1 = (Object[])o1, a2 = (Object[])o2;
|
||||
if (a1.Length != a2.Length)
|
||||
return false;
|
||||
for (uint i = 0; i < a1.Length; i++)
|
||||
if (!DeepEquals(a1[i],a2[i]))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
protected static string ToString(Object o)
|
||||
{
|
||||
if (o == null)
|
||||
{
|
||||
return "null";
|
||||
}
|
||||
if (o is byte[])
|
||||
{
|
||||
string ret = "<";
|
||||
byte[] a = (byte[])o;
|
||||
for (uint i = 0; i < a.Length; i++)
|
||||
{
|
||||
if (i != 0)
|
||||
ret += ".";
|
||||
ret += a[i].ToString("X2");
|
||||
}
|
||||
ret += ">";
|
||||
return ret;
|
||||
}
|
||||
else if (o is Object[])
|
||||
{
|
||||
Object[] a = (Object[])o;
|
||||
string ret = "[";
|
||||
|
||||
for (uint i = 0; i < a.Length; i++)
|
||||
{
|
||||
if (i != 0)
|
||||
ret += " ";
|
||||
ret += ToString(a[i]);
|
||||
}
|
||||
ret += "]";
|
||||
return ret;
|
||||
}
|
||||
else if (o is System.Int64)
|
||||
{
|
||||
System.Int64 i = (System.Int64)o;
|
||||
return (i < 0 ? "s-0x" : "s0x") + i.ToString("X");
|
||||
}
|
||||
else if (o is System.UInt64)
|
||||
{
|
||||
System.UInt64 i = (System.UInt64)o;
|
||||
return "u0x" + i.ToString("X");
|
||||
}
|
||||
else
|
||||
return "WAT(" + o.GetType() + ")";
|
||||
}
|
||||
|
||||
|
||||
internal static byte[] ToBytes(string s)
|
||||
{
|
||||
// Probably not what you want unless you're parsing binary data.
|
||||
// This is just a one-to-one encoding of the string's codepoints
|
||||
byte []ret = new byte[s.Length];
|
||||
for (int i = 0; i < s.Length; i++)
|
||||
{
|
||||
ret[i] = (byte)s[i];
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
protected void CheckParseOK(Parser p, string probe, Object expected)
|
||||
{
|
||||
|
||||
Object ret = p.Parse(ToBytes(probe));
|
||||
Assert.That(ret, Is.Not.Null);
|
||||
//System.Console.WriteLine(ToString(ret));
|
||||
//System.Console.WriteLine(ToString(expected));
|
||||
if (!DeepEquals(ret, expected))
|
||||
Assert.Fail();
|
||||
else
|
||||
Assert.Pass();
|
||||
}
|
||||
protected void CheckParseFail(Parser p, string probe)
|
||||
{
|
||||
|
||||
Object ret = p.Parse(ToBytes(probe));
|
||||
Assert.That(ret, Is.Null);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue