Skip to content

Commit 181727a

Browse files
authored
Merge pull request #79 from datalust/dev
2.2.0 Release
2 parents 585dd6a + c8eeff8 commit 181727a

File tree

24 files changed

+298
-57
lines changed

24 files changed

+298
-57
lines changed

.vscode/launch.json

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{
2+
"version": "0.2.0",
3+
"configurations": [
4+
{
5+
"name": "IntCalc",
6+
"type": "coreclr",
7+
"request": "launch",
8+
"preLaunchTask": "build",
9+
"program": "${workspaceFolder}/sample/IntCalc/bin/Debug/netcoreapp2.0/IntCalc.dll",
10+
"args": [ "IntCalc" ],
11+
"cwd": "${workspaceFolder}/sample/IntCalc",
12+
"console": "integratedTerminal",
13+
"stopAtEntry": false,
14+
"internalConsoleOptions": "openOnSessionStart"
15+
},
16+
{
17+
"name": "JsonParser",
18+
"type": "coreclr",
19+
"request": "launch",
20+
"preLaunchTask": "build",
21+
"program": "${workspaceFolder}/sample/JsonParser/bin/Debug/netcoreapp2.0/JsonParser.dll",
22+
"args": [ "IntCalc" ],
23+
"cwd": "${workspaceFolder}/sample/JsonParser",
24+
"console": "integratedTerminal",
25+
"stopAtEntry": false,
26+
"internalConsoleOptions": "openOnSessionStart"
27+
},
28+
{
29+
"name": "DateTimeTextParser",
30+
"type": "coreclr",
31+
"request": "launch",
32+
"preLaunchTask": "build",
33+
"program": "${workspaceFolder}/sample/DateTimeTextParser/bin/Debug/netcoreapp2.0/DateTimeParser.dll",
34+
"args": [ "IntCalc" ],
35+
"cwd": "${workspaceFolder}/sample/DateTimeTextParser",
36+
"console": "integratedTerminal",
37+
"stopAtEntry": false,
38+
"internalConsoleOptions": "openOnSessionStart"
39+
},
40+
{
41+
"name": ".NET Core Attach",
42+
"type": "coreclr",
43+
"request": "attach",
44+
"processId": "${command:pickProcess}"
45+
}
46+
]
47+
}

.vscode/tasks.json

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"version": "2.0.0",
3+
"tasks": [
4+
{
5+
"label": "build",
6+
"command": "dotnet",
7+
"type": "process",
8+
"group": {
9+
"kind": "build",
10+
"isDefault": true
11+
},
12+
"args": [
13+
"build",
14+
"/p:GenerateFullPaths=true"
15+
],
16+
"problemMatcher": "$msCompile"
17+
},
18+
{
19+
"label": "test",
20+
"command": "dotnet",
21+
"type": "process",
22+
"group": {
23+
"kind": "test",
24+
"isDefault": true
25+
},
26+
"args": [
27+
"test",
28+
"${workspaceFolder}/test/Superpower.Tests/Superpower.Tests.csproj",
29+
"/p:GenerateFullPaths=true"
30+
],
31+
"problemMatcher": "$msCompile"
32+
}
33+
]
34+
}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ Superpower is introduced, with a worked example, in [this blog post](https://nbl
240240
* [_DateTimeTextParser_](https://github.com/datalust/superpower/tree/dev/sample/DateTimeTextParser) shows how Superpower's text parsers work, parsing ISO-8601 date-times
241241
* [_IntCalc_](https://github.com/datalust/superpower/tree/dev/sample/IntCalc) is a simple arithmetic expresion parser (`1 + 2 * 3`) included in the repository, demonstrating how Superpower token parsing works
242242
* [_Plotty_](https://github.com/SuperJMN/Plotty) implements an instruction set for a RISC virtual machine
243+
* [_tcalc_](https://github.com/nblumhardt/tcalc) is an example expression language that computes durations (`1d / 12m`)
243244
244245
**Real-world** projects built with Superpower:
245246

appveyor.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ artifacts:
1010
deploy:
1111
- provider: NuGet
1212
api_key:
13-
secure: 2APsxl7cOn94Y5lvJ0LshnVGefMpKu/JUG6IiM6BK9UWq1bN0Zv4X4OBXzkMPFm0
13+
secure: Xi+qouQ6+cOJ85PMOQKEGy+MC1EKyYJCoF2YfuE9Rcj6lvNtm08TIZllTJCqId+M
1414
skip_symbols: true
1515
on:
1616
branch: /^(master|dev)$/

sample/IntCalc/ArithmeticExpressionTokenizer.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,14 @@ protected override IEnumerable<Result<ArithmeticExpressionToken>> Tokenize(TextS
2727
{
2828
ArithmeticExpressionToken charToken;
2929

30-
if (char.IsDigit(next.Value))
30+
var ch = next.Value;
31+
if (ch >= '0' && ch <= '9')
3132
{
3233
var integer = Numerics.Integer(next.Location);
3334
next = integer.Remainder.ConsumeChar();
3435
yield return Result.Value(ArithmeticExpressionToken.Number, integer.Location, integer.Remainder);
3536
}
36-
else if (_operators.TryGetValue(next.Value, out charToken))
37+
else if (_operators.TryGetValue(ch, out charToken))
3738
{
3839
yield return Result.Value(charToken, next.Location, next.Remainder);
3940
next = next.Remainder.ConsumeChar();

sample/IntCalc/IntCalc.csproj

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup Condition=" '$(OS)' != 'Windows_NT' ">
3+
<TargetFramework>netcoreapp1.0</TargetFramework>
4+
</PropertyGroup>
5+
<PropertyGroup Condition=" '$(OS)' == 'Windows_NT' ">
6+
<TargetFrameworks>net46;netcoreapp2.0</TargetFrameworks>
7+
</PropertyGroup>
28

39
<PropertyGroup>
4-
<TargetFrameworks>net46;netcoreapp1.0</TargetFrameworks>
510
<AssemblyName>IntCalc</AssemblyName>
611
<OutputType>Exe</OutputType>
712
<PackageId>IntCalc</PackageId>

src/Superpower/Combinators.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ public static TokenListParser<TKind, T[]> Many<TKind, T>(this TokenListParser<TK
410410
if (!r.Backtrack && r.IsPartial(@from))
411411
return TokenListParserResult.CastEmpty<TKind, T, T[]>(r);
412412

413-
return TokenListParserResult.Value(result.ToArray(), input, r.Remainder);
413+
return TokenListParserResult.Value(result.ToArray(), input, from);
414414
};
415415
}
416416

@@ -444,7 +444,7 @@ public static TextParser<T[]> Many<T>(this TextParser<T> parser)
444444
if (!r.Backtrack && r.IsPartial(from))
445445
return Result.CastEmpty<T, T[]>(r);
446446

447-
return Result.Value(result.ToArray(), input, r.Remainder);
447+
return Result.Value(result.ToArray(), input, from);
448448
};
449449
}
450450

@@ -476,7 +476,7 @@ public static TextParser<Unit> IgnoreMany<T>(this TextParser<T> parser)
476476
if (!r.Backtrack && r.IsPartial(from))
477477
return Result.CastEmpty<T, Unit>(r);
478478

479-
return Result.Value(Unit.Value, input, r.Remainder);
479+
return Result.Value(Unit.Value, input, from);
480480
};
481481
}
482482

src/Superpower/Display/Presentation.cs

Lines changed: 62 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -69,26 +69,76 @@ public static string FormatAppearance<TKind>(TKind kind, string value)
6969

7070
return $"{FormatKind(kind)} {clipped}";
7171
}
72-
7372
public static string FormatLiteral(char literal)
7473
{
7574
switch (literal)
7675
{
77-
case '\r':
78-
return "carriage return";
79-
case '\n':
80-
return "line feed";
81-
case '\t':
82-
return "tab";
83-
case '\0':
84-
return "NUL";
85-
default:
86-
return "`" + literal + "`";
76+
//Unicode Category: Space Separators
77+
case '\x00A0': return "U+00A0 no-break space";
78+
case '\x1680': return "U+1680 ogham space mark";
79+
case '\x2000': return "U+2000 en quad";
80+
case '\x2001': return "U+2001 em quad";
81+
case '\x2002': return "U+2002 en space";
82+
case '\x2003': return "U+2003 em space";
83+
case '\x2004': return "U+2004 three-per-em space";
84+
case '\x2005': return "U+2005 four-per-em space";
85+
case '\x2006': return "U+2006 six-per-em space";
86+
case '\x2007': return "U+2007 figure space";
87+
case '\x2008': return "U+2008 punctuation space";
88+
case '\x2009': return "U+2009 thin space";
89+
case '\x200A': return "U+200A hair space";
90+
case '\x202F': return "U+202F narrow no-break space";
91+
case '\x205F': return "U+205F medium mathematical space";
92+
case '\x3000': return "U+3000 ideographic space";
93+
94+
//Line Separator
95+
case '\x2028': return "U+2028 line separator";
96+
97+
//Paragraph Separator
98+
case '\x2029': return "U+2029 paragraph separator";
99+
100+
//Unicode C0 Control Codes (ASCII equivalent)
101+
case '\x0000': return "NUL"; //\0
102+
case '\x0001': return "U+0001 start of heading";
103+
case '\x0002': return "U+0002 start of text";
104+
case '\x0003': return "U+0003 end of text";
105+
case '\x0004': return "U+0004 end of transmission";
106+
case '\x0005': return "U+0005 enquiry";
107+
case '\x0006': return "U+0006 acknowledge";
108+
case '\x0007': return "U+0007 bell";
109+
case '\x0008': return "U+0008 backspace";
110+
case '\x0009': return "tab"; //\t
111+
case '\x000A': return "line feed"; //\n
112+
case '\x000B': return "U+000B vertical tab";
113+
case '\x000C': return "U+000C form feed";
114+
case '\x000D': return "carriage return"; //\r
115+
case '\x000E': return "U+000E shift in";
116+
case '\x000F': return "U+000F shift out";
117+
case '\x0010': return "U+0010 data link escape";
118+
case '\x0011': return "U+0011 device ctrl 1";
119+
case '\x0012': return "U+0012 device ctrl 2";
120+
case '\x0013': return "U+0013 device ctrl 3";
121+
case '\x0014': return "U+0014 device ctrl 4";
122+
case '\x0015': return "U+0015 not acknowledge";
123+
case '\x0016': return "U+0016 synchronous idle";
124+
case '\x0017': return "U+0017 end transmission block";
125+
case '\x0018': return "U+0018 cancel";
126+
case '\x0019': return "U+0019 end of medium";
127+
case '\x0020': return "space";
128+
case '\x001A': return "U+001A substitute";
129+
case '\x001B': return "U+001B escape";
130+
case '\x001C': return "U+001C file separator";
131+
case '\x001D': return "U+001D group separator";
132+
case '\x001E': return "U+001E record separator";
133+
case '\x001F': return "U+001F unit separator";
134+
case '\x007F': return "U+007F delete";
135+
136+
default: return "`" + literal + "`";
87137
}
88138
}
89139

90140
public static string FormatLiteral(string literal)
91-
{
141+
{
92142
return "`" + literal + "`";
93143
}
94144
}

src/Superpower/Model/Result.cs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
using Superpower.Util;
16+
1517
namespace Superpower.Model
1618
{
1719
/// <summary>
@@ -95,14 +97,7 @@ public static Result<T> CombineEmpty<T>(Result<T> first, Result<T> second)
9597
if (expectations == null)
9698
expectations = second.Expectations;
9799
else if (second.Expectations != null)
98-
{
99-
expectations = new string[first.Expectations.Length + second.Expectations.Length];
100-
var i = 0;
101-
for (; i < first.Expectations.Length; ++i)
102-
expectations[i] = first.Expectations[i];
103-
for (var j = 0; j < second.Expectations.Length; ++i, ++j)
104-
expectations[i] = second.Expectations[j];
105-
}
100+
expectations = ArrayEnumerable.Concat(first.Expectations, second.Expectations);
106101

107102
return new Result<T>(second.Remainder, second.ErrorMessage, expectations, second.Backtrack);
108103
}

src/Superpower/Model/Result`1.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public T Value
6868
get
6969
{
7070
if (!HasValue)
71-
throw new InvalidOperationException("Result has no value.");
71+
throw new InvalidOperationException($"{nameof(Result)} has no value.");
7272
return _value;
7373
}
7474
}
@@ -141,7 +141,7 @@ public string FormatErrorMessageFragment()
141141
else
142142
{
143143
var next = Location.ConsumeChar().Value;
144-
message = $"unexpected `{next}`";
144+
message = $"unexpected {Display.Presentation.FormatLiteral(next)}";
145145
}
146146

147147
if (Expectations != null)

0 commit comments

Comments
 (0)