Mercurial > pub > ImplabNet
comparison Implab/Formats/JSON/JSONGrammar.cs @ 190:1c2a16d071a7 v2
Слияние с ref20160224
author | cin |
---|---|
date | Fri, 22 Apr 2016 13:08:08 +0300 |
parents | 4f82e0f161c3 |
children |
comparison
equal
deleted
inserted
replaced
161:2a8466f0cb8a | 190:1c2a16d071a7 |
---|---|
1 using System.Linq; | |
2 using Implab.Automaton.RegularExpressions; | |
3 using System; | |
4 using Implab.Automaton; | |
5 using Implab.Components; | |
6 | |
7 namespace Implab.Formats.JSON { | |
8 class JSONGrammar : Grammar<char> { | |
9 public enum TokenType { | |
10 None, | |
11 BeginObject, | |
12 EndObject, | |
13 BeginArray, | |
14 EndArray, | |
15 String, | |
16 Number, | |
17 Literal, | |
18 NameSeparator, | |
19 ValueSeparator, | |
20 Whitespace, | |
21 | |
22 StringBound, | |
23 EscapedChar, | |
24 UnescapedChar, | |
25 EscapedUnicode | |
26 } | |
27 | |
28 static LazyAndWeak<JSONGrammar> _instance = new LazyAndWeak<JSONGrammar>(() => new JSONGrammar()); | |
29 | |
30 public static JSONGrammar Instance { | |
31 get { return _instance.Value; } | |
32 } | |
33 | |
34 readonly ScannerContext<TokenType> m_jsonExpression; | |
35 readonly ScannerContext<TokenType> m_stringExpression; | |
36 readonly CharAlphabet m_defaultAlphabet = new CharAlphabet(); | |
37 | |
38 public JSONGrammar() { | |
39 DefineAlphabet(Enumerable.Range(0, 0x20).Select(x => (char)x)); | |
40 var hexDigit = SymbolRangeToken('a','f').Or(SymbolRangeToken('A','F')).Or(SymbolRangeToken('0','9')); | |
41 var digit9 = SymbolRangeToken('1', '9'); | |
42 var zero = SymbolToken('0'); | |
43 var digit = zero.Or(digit9); | |
44 var dot = SymbolToken('.'); | |
45 var minus = SymbolToken('-'); | |
46 var sign = SymbolSetToken('-', '+'); | |
47 var expSign = SymbolSetToken('e', 'E'); | |
48 var letters = SymbolRangeToken('a', 'z'); | |
49 var integer = zero.Or(digit9.Cat(digit.EClosure())); | |
50 var frac = dot.Cat(digit.Closure()); | |
51 var exp = expSign.Cat(sign.Optional()).Cat(digit.Closure()); | |
52 var quote = SymbolToken('"'); | |
53 var backSlash = SymbolToken('\\'); | |
54 var specialEscapeChars = SymbolSetToken('\\', '"', '/', 'b', 'f', 't', 'n', 'r'); | |
55 var unicodeEspace = SymbolToken('u').Cat(hexDigit.Repeat(4)); | |
56 var whitespace = SymbolSetToken('\n', '\r', '\t', ' ').EClosure(); | |
57 var beginObject = whitespace.Cat(SymbolToken('{')).Cat(whitespace); | |
58 var endObject = whitespace.Cat(SymbolToken('}')).Cat(whitespace); | |
59 var beginArray = whitespace.Cat(SymbolToken('[')).Cat(whitespace); | |
60 var endArray = whitespace.Cat(SymbolToken(']')).Cat(whitespace); | |
61 var nameSep = whitespace.Cat(SymbolToken(':')).Cat(whitespace); | |
62 var valueSep = whitespace.Cat(SymbolToken(',')).Cat(whitespace); | |
63 | |
64 var number = minus.Optional().Cat(integer).Cat(frac.Optional()).Cat(exp.Optional()); | |
65 var literal = letters.Closure(); | |
66 var unescaped = SymbolTokenExcept(Enumerable.Range(0, 0x20).Union(new int[] { '\\', '"' }).Select(x => (char)x)); | |
67 | |
68 var jsonExpression = | |
69 number.Tag(TokenType.Number) | |
70 .Or(literal.Tag(TokenType.Literal)) | |
71 .Or(quote.Tag(TokenType.StringBound)) | |
72 .Or(beginObject.Tag(TokenType.BeginObject)) | |
73 .Or(endObject.Tag(TokenType.EndObject)) | |
74 .Or(beginArray.Tag(TokenType.BeginArray)) | |
75 .Or(endArray.Tag(TokenType.EndArray)) | |
76 .Or(nameSep.Tag(TokenType.NameSeparator)) | |
77 .Or(valueSep.Tag(TokenType.ValueSeparator)) | |
78 .Or(SymbolSetToken('\n', '\r', '\t', ' ').Closure().Tag(TokenType.Whitespace)); | |
79 | |
80 | |
81 var jsonStringExpression = | |
82 quote.Tag(TokenType.StringBound) | |
83 .Or(backSlash.Cat(specialEscapeChars).Tag(TokenType.EscapedChar)) | |
84 .Or(backSlash.Cat(unicodeEspace).Tag(TokenType.EscapedUnicode)) | |
85 .Or(unescaped.Closure().Tag(TokenType.UnescapedChar)); | |
86 | |
87 | |
88 m_jsonExpression = BuildScannerContext<TokenType>(jsonExpression); | |
89 m_stringExpression = BuildScannerContext<TokenType>(jsonStringExpression); | |
90 | |
91 | |
92 } | |
93 | |
94 protected override IAlphabetBuilder<char> AlphabetBuilder { | |
95 get { | |
96 return m_defaultAlphabet; | |
97 } | |
98 } | |
99 | |
100 public ScannerContext<TokenType> JsonExpression { | |
101 get { | |
102 return m_jsonExpression; | |
103 } | |
104 } | |
105 | |
106 public ScannerContext<TokenType> JsonStringExpression { | |
107 get { | |
108 return m_stringExpression; | |
109 } | |
110 } | |
111 | |
112 Token SymbolRangeToken(char start, char stop) { | |
113 return SymbolToken(Enumerable.Range(start, stop - start + 1).Select(x => (char)x)); | |
114 } | |
115 | |
116 protected override IndexedAlphabetBase<char> CreateAlphabet() { | |
117 return new CharAlphabet(); | |
118 } | |
119 | |
120 } | |
121 } |