-
Notifications
You must be signed in to change notification settings - Fork 97
/
Comment.cs
103 lines (90 loc) · 3.79 KB
/
Comment.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright 2016 Datalust, Superpower Contributors, Sprache Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using Superpower.Model;
namespace Superpower.Parsers
{
/// <summary>
/// Parsers for matching comments in various styles.
/// </summary>
public static class Comment
{
/// <summary>
/// Parses a comment that begins with a specified pattern and continues to the end of the line.
/// </summary>
/// <remarks>
/// The comment span does not include the end-of-line characters that terminate it.
/// </remarks>
/// <param name="beginComment">Recognizes the beginning of the comment.</param>
/// <returns>The span covered by the comment.</returns>
public static TextParser<TextSpan> ToEndOfLine(TextParser<TextSpan> beginComment)
{
return i =>
{
var begin = beginComment(i);
if (!begin.HasValue)
return begin;
var remainder = begin.Remainder;
while (!remainder.IsAtEnd)
{
var ch = remainder.ConsumeChar();
if (ch.Value == '\r' || ch.Value == '\n')
break;
remainder = ch.Remainder;
}
return Result.Value(i.Until(remainder), i, remainder);
};
}
/// <summary>
/// Parses a C++ style comment, beginning with a double forward slash `//`
/// and continuing to the end of the line.
/// </summary>
public static TextParser<TextSpan> CPlusPlusStyle { get; } = ToEndOfLine(Span.EqualTo("//"));
/// <summary>
/// Parses a SQL style comment, beginning with a double dash `--`
/// and continuing to the end of the line.
/// </summary>
public static TextParser<TextSpan> SqlStyle { get; } = ToEndOfLine(Span.EqualTo("--"));
/// <summary>
/// Parses a shell style comment, beginning with a pound/hash `#` sign
/// and continuing to the end of the line.
/// </summary>
public static TextParser<TextSpan> ShellStyle { get; } = ToEndOfLine(Span.EqualTo("#"));
/// <summary>
/// Parses a C-style multiline comment beginning with `/*` and ending with `*/`.
/// </summary>
public static TextParser<TextSpan> CStyle
{
get
{
var beginComment = Span.EqualTo("/*");
var endComment = Span.EqualTo("*/");
return i =>
{
var begin = beginComment(i);
if (!begin.HasValue)
return begin;
var content = begin.Remainder;
while (!content.IsAtEnd)
{
var end = endComment(content);
if (end.HasValue)
return Result.Value(i.Until(end.Remainder), i, end.Remainder);
content = content.ConsumeChar().Remainder;
}
return endComment(content); // Will fail, because we're at the end-of-input.
};
}
}
}
}