14 using System.Collections.Generic;
16 using System.Globalization;
49 internal enum IdFlagsInternal :
short {
57 #region constructors and initialization
64 AllFirstChars = Strings.AllLatinLetters + extraFirstChars;
65 AllChars = Strings.AllLatinLetters + Strings.DecimalDigits + extraChars;
69 base.AddPrefixFlag(prefix, (short)options);
73 #region properties: AllChars, AllFirstChars
88 base.Init(grammarData);
89 AllChars = AllChars?? String.Empty;
90 AllFirstChars = AllFirstChars ?? string.Empty;
94 case CaseRestriction.AllLower:
95 case CaseRestriction.FirstLower:
96 AllFirstChars = AllFirstChars.ToLower();
98 case CaseRestriction.AllUpper:
99 case CaseRestriction.FirstUpper:
100 AllFirstChars = AllFirstChars.ToUpper();
104 if (this.StartCharCategories.Count > 0)
105 Grammar.FallbackTerminals.Add(
this);
106 if (this.EditorInfo == null)
109 this.AstNodeType = typeof(Irony.Interpreter.Ast.IdentifierNode);
115 list.AddRange(Prefixes);
116 if (
string.IsNullOrEmpty(AllFirstChars))
118 char[] chars = AllFirstChars.ToCharArray();
119 foreach (
char ch
in chars)
120 list.Add(ch.ToString());
121 if ((Options &
IdOptions.CanStartWithEscape) != 0)
122 list.Add(this.EscapeChar.ToString());
126 private void AdjustCasing() {
128 case CaseRestriction.None:
break;
129 case CaseRestriction.FirstLower:
130 AllFirstChars = AllFirstChars.ToLower();
132 case CaseRestriction.FirstUpper:
133 AllFirstChars = AllFirstChars.ToUpper();
135 case CaseRestriction.AllLower:
136 AllFirstChars = AllFirstChars.ToLower();
137 AllChars = AllChars.ToLower();
139 case CaseRestriction.AllUpper:
140 AllFirstChars = AllFirstChars.ToUpper();
141 AllChars = AllChars.ToUpper();
147 base.InitDetails(context, details);
148 details.Flags = (short)Options;
153 Token token = base.CreateToken(context, source, details);
157 CheckReservedWord(token);
160 private void CheckReservedWord(
Token token) {
163 token.KeyTerm = keyTerm;
165 if (keyTerm.FlagIsSet(
TermFlags.IsReservedWord))
173 source.PreviewPosition++;
174 while (AllChars.IndexOf(source.
PreviewChar) >= 0 && !source.EOF())
175 source.PreviewPosition++;
178 var token = source.CreateToken(this.OutputTerminal);
185 CheckReservedWord(token);
190 int start = source.PreviewPosition;
191 bool allowEscapes = details.IsSet((short)
IdOptions.AllowsEscapes);
193 while (!source.
EOF()) {
194 char current = source.PreviewChar;
196 if (allowEscapes && current == this.EscapeChar) {
197 current = ReadUnicodeEscape(source, details);
201 source.PreviewPosition--;
202 if (details.
Error != null)
209 UnicodeCategory currCat = char.GetUnicodeCategory(current);
210 if (!this.CharsToRemoveCategories.Contains(currCat))
211 outputChars.Add(current);
212 source.PreviewPosition++;
214 if (outputChars.Count == 0)
217 details.Body =
new string(outputChars.ToArray());
218 if (!CheckCaseRestriction(details.
Body))
220 return !string.IsNullOrEmpty(details.Body);
223 private bool CharOk(
char ch,
bool first) {
225 string all = first? AllFirstChars : AllChars;
226 if(all.IndexOf(ch) >= 0)
return true;
228 UnicodeCategory chCat = char.GetUnicodeCategory(ch);
230 if (catList.Contains(chCat))
return true;
234 private bool CheckCaseRestriction(
string body) {
236 case CaseRestriction.FirstLower:
return Char.IsLower(body, 0);
237 case CaseRestriction.FirstUpper:
return Char.IsUpper(body, 0);
238 case CaseRestriction.AllLower:
return body.ToLower() == body;
239 case CaseRestriction.AllUpper:
return body.ToUpper() == body;
240 default :
return true;
245 private char ReadUnicodeEscape(ISourceStream source, CompoundTokenDetails details) {
247 source.PreviewPosition++;
249 switch (source.PreviewChar) {
250 case 'u': len = 4;
break;
251 case 'U': len = 8;
break;
256 if (source.PreviewPosition + len > source.Text.Length) {
260 source.PreviewPosition++;
261 string digits = source.Text.Substring(source.PreviewPosition, len);
262 char result = (char)
Convert.ToUInt32(digits, 16);
263 source.PreviewPosition += len;
264 details.Flags |= (int) IdFlagsInternal.HasEscapes;
void SetTerminal(Terminal terminal)
Sets the terminal.
override IList< string > GetFirsts()
CaseRestriction CaseRestriction
char PreviewChar
Gets a char at preview position
bool FlagIsSet(LanguageFlags flag)
override void Init(GrammarData grammarData)
static string ErrInvEscSymbol
Looks up a localized string similar to Invalid escape symbol, expected 'u' or 'U' only...
IdentifierTerminal(string name)
override bool ReadBody(ISourceStream source, CompoundTokenDetails details)
override void InitDetails(ParsingContext context, CompoundTokenDetails details)
string Text
Gets the text associated with this token.
Interface for Terminals to access the source stream and produce tokens.
int PreviewPosition
Gets or sets the current preview position in the source file. Must be greater or equal to Location...
Flags
Enumeration of the new Assimp's flags.
delegate void AstNodeCreator(ParsingContext context, ParseTreeNode parseNode)
IdentifierTerminal(string name, IdOptions options)
static string ErrInvEscSeq
Looks up a localized string similar to Invalid escape sequence..
override Token QuickParse(ParsingContext context, ISourceStream source)
void AddPrefix(string prefix, IdOptions options)
string WhitespaceAndDelimiters
HRESULT Convert(_In_ const Image &srcImage, _In_ DXGI_FORMAT format, _In_ DWORD filter, _In_ float threshold, _Out_ ScratchImage &image)
override bool ConvertValue(CompoundTokenDetails details)
override Token CreateToken(ParsingContext context, ISourceStream source, CompoundTokenDetails details)
Tokens are produced by scanner and fed to parser, optionally passing through Token filters in between...
IdentifierTerminal(string name, string extraChars, string extraFirstChars)