private void CheckParseRequest(ParseRequest req) { try { var lexer = new AphidLexer(req.Text); var parser = new AphidParser(lexer.GetTokens()); parser.Parse(); } catch (AphidParserException e) { var lineCol = TokenHelper.GetIndexPosition(req.Text, e.UnexpectedToken.Index); var span = new TextSpan() { iStartLine = lineCol.Item1, iEndLine = lineCol.Item1, iStartIndex = lineCol.Item2, iEndIndex = lineCol.Item2 + (e.UnexpectedToken.Lexeme != null ? e.UnexpectedToken.Lexeme.Length : 0) }; var msg = string.Format( "Unexpected {0}: {1}, expected {2}", e.UnexpectedToken.TokenType.ToString(), e.UnexpectedToken.Lexeme, e.ExpectedToken); req.Sink.AddError(req.FileName, msg, span, Severity.Error); } }
private void WriteError( IVsGeneratorProgress progress, string code, Exception exception, AphidExpression expression = null) { Tuple <int, int> pos = null; if (expression?.Index > -1) { pos = TokenHelper.GetIndexPosition( code, expression.Index); } progress.GeneratorError( 0, 0, exception.ToString(), pos == null || pos.Item1 == -1 ? 0xffffffffu : (uint)pos.Item1, pos == null || pos.Item2 == -1 ? 0xffffffffu : (uint)pos.Item2); }
public int Generate( string wszInputFilePath, string bstrInputFileContents, string wszDefaultNamespace, IntPtr[] rgbOutputFileContents, out uint pcbOutput, IVsGeneratorProgress pGenerateProgress) { if (bstrInputFileContents == null) { throw new ArgumentException(bstrInputFileContents); } string csOut = null; var result = VSConstants.E_FAIL; try { #if RELEASE if (AphidBuildInterop.Compile == null) { var interpreter = new AphidInterpreter(); var interopScript = Path.Combine( Path.GetDirectoryName(typeof(AphidExpression).Assembly.Location), "AphidBuildInterop.alx"); interpreter.InterpretFile(interopScript); } #else _interopScript.Refresh(); if (_lastRun < _interopScript.LastWriteTime) { _lastRun = DateTime.Now; var interpreter = new AphidInterpreter(); interpreter.InterpretFile(_interopScript.FullName); } #endif csOut = AphidBuildInterop.Compile( wszInputFilePath, bstrInputFileContents, wszDefaultNamespace); result = VSConstants.S_OK; } catch (AphidParserException e) { if (e.UnexpectedToken.TokenType != default(AphidToken).TokenType) { var pos = TokenHelper.GetIndexPosition( bstrInputFileContents, e.UnexpectedToken.Index); pGenerateProgress.GeneratorError( 0, 0, ParserErrorMessage.Create(bstrInputFileContents, e, false), pos == null || pos.Item1 == -1 ? 0xffffffffu : (uint)pos.Item1, pos == null || pos.Item2 == -1 ? 0xffffffffu : (uint)pos.Item2); } else { WriteError( pGenerateProgress, bstrInputFileContents, e, e.Expression); } } catch (AphidLoadScriptException e) { WriteError( pGenerateProgress, bstrInputFileContents, e, e.CurrentExpression ?? e.CurrentStatement); } catch (AphidRuntimeException e) { WriteError( pGenerateProgress, bstrInputFileContents, e, e.CurrentExpression ?? e.CurrentStatement); } catch (Exception e) { WriteError( pGenerateProgress, bstrInputFileContents, e); } var bytes = csOut?.GetBytes(); if (bytes == null || bytes.Length == 0) { rgbOutputFileContents[0] = IntPtr.Zero; pcbOutput = 0; } else { rgbOutputFileContents[0] = Marshal.AllocCoTaskMem(bytes.Length); Marshal.Copy(bytes, 0, rgbOutputFileContents[0], bytes.Length); pcbOutput = (uint)bytes.Length; } //pGenerateProgress.GeneratorError( return(result); }
public override AuthoringScope ParseSource(ParseRequest req) { Debug.WriteLine("Parse reason: {0}", req.Reason); var scope = new AphidAuthoringScope { Identifiers = GetTokens(req.Text) .Where(x => x.TokenType == AphidTokenType.Identifier) .Select(x => x.Lexeme) .Distinct() .ToArray() }; switch (req.Reason) { case ParseReason.QuickInfo: break; case ParseReason.MemberSelect: break; case ParseReason.DisplayMemberList: break; case ParseReason.CompleteWord: break; case ParseReason.MatchBraces: case ParseReason.MemberSelectAndHighlightBraces: case ParseReason.HighlightBraces: var braces = TokenHelper.GetBraces( req.Text, req.Line, req.Col - 1); if (braces != null) { req.Sink.MatchPair( CreateSpan(braces[0][0], braces[0][1]), CreateSpan(braces[1][0], braces[1][1]), 1); return(scope); } var index = TokenHelper.GetIndex(req.Text, req.Line, req.Col - 1); var str = req.Text.Substring(index); var tokens = new AphidLexer(str).GetAllTokens(); var depth = 1; var rightBraceIndex = -1; var braceType = tokens[0].TokenType; var i = 1; switch (braceType) { case AphidTokenType.LeftBrace: for (; i < tokens.Count; i++) { switch (tokens[i].TokenType) { case AphidTokenType.LeftBrace: depth++; break; case AphidTokenType.RightBrace: depth--; break; } if (depth == 0) { rightBraceIndex = index + tokens[i].Index; break; } } break; case AphidTokenType.LeftParenthesis: for (; i < tokens.Count; i++) { switch (tokens[i].TokenType) { case AphidTokenType.LeftParenthesis: depth++; break; case AphidTokenType.RightParenthesis: depth--; break; } if (depth == 0) { rightBraceIndex = index + tokens[i].Index; break; } } break; case AphidTokenType.LeftBracket: for (; i < tokens.Count; i++) { switch (tokens[i].TokenType) { case AphidTokenType.LeftBracket: depth++; break; case AphidTokenType.RightBracket: depth--; break; } if (depth == 0) { rightBraceIndex = index + tokens[i].Index; break; } } break; //Todo: handle other direction } if (rightBraceIndex != -1) { var rightLineCol = TokenHelper.GetIndexPosition(req.Text, rightBraceIndex); req.Sink.MatchPair( CreateSpan(req.Line, req.Col - 1), CreateSpan(rightLineCol.Item1, rightLineCol.Item2), 1); } break; case ParseReason.Check: CheckParseRequest(req); break; default: break; } return(scope); }