protected override LiteralNode ParseLiteral(ILiteralNodeParent parent, Token token) { if ((parent is ArrayLiteralNode) && Parser.IsOpeningParenthesis(token)) { // Stupid VSE allows declarations of arrays like: #( 1 2 ( 3 4 ) 5 6), // which is identical to: #( 1 2 #( 3 4 ) 5 6). // Only the inner (child) arrays may omit the hash prefix. // Here we emulate this and create a 'fake' hash token. // The fake hash token gets the same source positions and the parenthesis token. SpecialCharacterToken hash = new SpecialCharacterToken(SemanticConstants.LiteralArrayPrefix); hash.SetTokenValues(token.StartPosition, token.StopPosition, null); this.ResidueToken = token; return(this.ParseArrayLiteral(parent, hash)); } if (!Parser.IsLiteralArrayPrefix(token)) { return(base.ParseLiteral(parent, token)); } Token token2 = this.GetNextTokenxx(Preference.Default); if (VseCompatibleParser.IsOpeningByteArrayBracket(token2)) { return(this.ParseByteArrayLiteral(parent, (SpecialCharacterToken)token, (SpecialCharacterToken)token2)); } this.ResidueToken = token2; return(base.ParseLiteral(parent, token)); }
/// <summary> /// Initializes the node after being parsed by the parser. /// </summary> /// <param name="elements">Collection with literal nodes that compose the elements of the literal array.</param> /// <param name="rightParenthesis">Closing right parenthesis of the literal array.</param> protected internal void SetContents(IEnumerable <SmallIntegerLiteralNode> elements, SpecialCharacterToken rightBracket) { if (elements == null) { throw new ArgumentNullException("elements"); } if ((rightBracket != null) && !VseCompatibleParser.IsClosingByteArrayBracket(rightBracket)) // NB: We allow null value. { throw new ArgumentException("rightBracket"); } this.RightBracket = rightBracket; foreach (SmallIntegerLiteralNode elem in elements) { this.Elements.Add(elem); } }
/// <summary> /// Create and initialize a new literal array node. /// </summary> /// <param name="parent">Parent node that defines this literal node.</param> /// <param name="arrayToken">Hash token that defines the start of array literals.</param> /// <param name="leftParenthesis">Opening left parenthesis of the literal array.</param> protected internal ByteArrayLiteralNode(ILiteralNodeParent parent, SpecialCharacterToken arrayToken, SpecialCharacterToken leftBracket) : base(parent) { #if DEBUG if (!Parser.IsLiteralArrayPrefix(arrayToken)) { throw new ArgumentException("arrayToken"); } if (!VseCompatibleParser.IsOpeningByteArrayBracket(leftBracket)) { throw new ArgumentException("leftBracket"); } #endif this.ArrayToken = arrayToken; this.LeftBracket = leftBracket; this.Elements = new List <SmallIntegerLiteralNode>(); }
private void ParseMethod(string src, int position, ref int processed, ref int errors) { if (String.IsNullOrWhiteSpace(src)) return; StringReader reader = new StringReader(src); bool ok = true; string err = null; try { Parser parser = new VseCompatibleParser(); this.Errors = new Dictionary<object, List<string>>(); parser.ErrorSink = this; MethodNode node = parser.ParseMethod(reader); ok = (this.Errors.Count == 0); if (!ok) err = "Errors: " + this.Errors.Count.ToString(); } catch (Exception ex) { ok = false; err = ex.Message; } if (!ok) this.listErrors.Items.Add("Pos: " + position + ", " + err).Tag = src; if (!ok) errors++; processed++; }
protected ByteArrayLiteralNode ParseByteArrayLiteral(ILiteralNodeParent parent, SpecialCharacterToken arrayToken, SpecialCharacterToken leftBracket) { // PARSE: <array literal> ::= '#[' <number literal>* ']' List <SmallIntegerLiteralNode> elements = new List <SmallIntegerLiteralNode>(); ByteArrayLiteralNode result = new ByteArrayLiteralNode(parent, arrayToken, leftBracket); // Process tokens inside the array ... while (true) { // ... get next token in the array ... Token token = this.GetNextTokenxx(Preference.NegativeSign); // Is this closing parenthesis? if (VseCompatibleParser.IsClosingByteArrayBracket(token)) { // Closing parenthesis ... done with the array, return litral array node. result.SetContents(elements, (SpecialCharacterToken)token); this.ResidueToken = null; return(result); } if (token is EofToken) { // Unfinished source code ... return result.SetContents(elements, null); this.ReportParserError(parent, "Missing literal byte array closing bracket.", token); this.ResidueToken = token; return(result); } // PARSE: <numeric liteal> if (token is SmallIntegerToken) { elements.Add(new SmallIntegerLiteralNode(result, (SmallIntegerToken)token, null)); } else if (token is NegativeSignToken) { NegativeSignToken negativeSign = (NegativeSignToken)token; token = this.GetNextTokenxx(Preference.NegativeSign); if (token is SmallIntegerToken) { elements.Add(new SmallIntegerLiteralNode(result, (SmallIntegerToken)token, negativeSign)); } else { this.ReportParserError(parent, "Unrecognized literal.", token); this.ResidueToken = token; result.SetContents(elements, null); return(result); } } else { this.ReportParserError(parent, "Unrecognized literal.", token); this.ResidueToken = token; result.SetContents(elements, null); return(result); } } }