protected override void ReParseImpl() { var outputWindow = OutputWindowService.TryGetPane(PredefinedOutputWindowPanes.TvlIntellisense); Stopwatch stopwatch = Stopwatch.StartNew(); string filename = "<Unknown File>"; ITextDocument textDocument = TextDocument; if (textDocument != null) filename = textDocument.FilePath; var snapshot = TextBuffer.CurrentSnapshot; var input = new SnapshotCharStream(snapshot, new Span(0, snapshot.Length)); var lexer = new PhpLexer(input); lexer.TokenFactory = new SnapshotTokenFactory(snapshot, lexer); var tokens = new CommonTokenStream(lexer); var parser = new PhpParser(tokens); parser.BuildParseTree = true; List<ParseErrorEventArgs> errors = new List<ParseErrorEventArgs>(); parser.AddErrorListener(new ErrorListener(filename, errors, outputWindow)); var result = parser.compileUnit(); OutliningTreesListener listener = new OutliningTreesListener(); ParseTreeWalker.Default.Walk(listener, result); OnParseComplete(new PhpOutliningParseResultEventArgs(snapshot, errors, stopwatch.Elapsed, tokens.GetTokens(), result, listener.OutliningTrees)); }
protected override void ReParseImpl() { var outputWindow = OutputWindowService.TryGetPane(PredefinedOutputWindowPanes.TvlIntellisense); Stopwatch stopwatch = Stopwatch.StartNew(); string filename = "<Unknown File>"; ITextDocument textDocument = TextDocument; if (textDocument != null) { filename = textDocument.FilePath; } var snapshot = TextBuffer.CurrentSnapshot; var input = new SnapshotCharStream(snapshot, new Span(0, snapshot.Length)); var lexer = new PhpLexer(input); lexer.TokenFactory = new SnapshotTokenFactory(snapshot, lexer); var tokens = new CommonTokenStream(lexer); var parser = new PhpParser(tokens); parser.BuildParseTree = true; List <ParseErrorEventArgs> errors = new List <ParseErrorEventArgs>(); parser.AddErrorListener(new ErrorListener(filename, errors, outputWindow)); var result = parser.compileUnit(); NavigationTreesListener listener = new NavigationTreesListener(); ParseTreeWalker.Default.Walk(listener, result); OnParseComplete(new PhpEditorNavigationParseResultEventArgs(snapshot, errors, stopwatch.Elapsed, tokens.GetTokens(), result, listener.NavigationTrees)); }
static void Main(string[] args) { try { var excerpt = @"<?php $heredocString = <<<TXT HEREDOC TEXT TXT; $heredocString = <<<TXT HEREDOC TEXT TXT ; "; var inputStream = new AntlrInputStream(excerpt); var lexer = new PhpLexer(inputStream); var commonTokenStream = new CommonTokenStream(lexer); var parser = new PhpParser(commonTokenStream); parser.htmlDocument(); var writer = new TokenStreamRewriter(commonTokenStream); var text = writer.GetText(); Console.WriteLine(text); } catch (Exception Ex) { Console.Error.WriteLine(Ex.Message); } }
public override StaticAnalysisAlert[] GetAlerts(string code, PhpToken[] tokens) { var functions = PhpParser.GetGlobalFunctionCalls(tokens); var sanitizationFunctions = GetSanitizationFunctions(); var targetFunctionNames = GetFunctions(); var targetFunctionCalls = functions .Where(x => targetFunctionNames.Contains(x.Id.Lexeme) && !x.ParamTokens.Any(y => sanitizationFunctions.Contains(y.Lexeme))); var insecureCalls = targetFunctionCalls.Where(IsCallInsecure); return(insecureCalls .Select(x => { var line = code.GetLineNumber(x.Id.Index); var code2 = code.InsertLineNumbers(); var index = code2.GetLineIndex(line); return new StaticAnalysisAlert(Name, line + 1, code2.GetSurroundingLines(index, 9, 9)); }) .ToArray()); }
public override void EnterFunctionDefinition(PhpParser.FunctionDefinitionContext context) { var codeBlock = context.codeBlock(); if (codeBlock != null) _outliningTrees.Add(context.codeBlock()); }
public override void EnterClassOrInterfaceDefinition(PhpParser.ClassOrInterfaceDefinitionContext context) { var codeBlock = context.codeBlock(); if (codeBlock != null) _outliningTrees.Add(context.codeBlock()); }
public override void EnterCode(PhpParser.CodeContext context) { _outliningTrees.Add(context); }
public override void EnterFunctionDefinition(PhpParser.FunctionDefinitionContext context) { _navigationTrees.Add(context); }
public override void EnterClassOrInterfaceDefinition(PhpParser.ClassOrInterfaceDefinitionContext context) { _navigationTrees.Add(context); }
private IEnumerable<string> ProcessArguments(PhpParser.FunctionParameterListContext context) { if (context == null) yield break; PhpParser.FunctionParametersContext functionParametersContext = context.functionParameters(); if (functionParametersContext == null) yield break; foreach (PhpParser.FunctionParameterContext argTree in functionParametersContext.functionParameter()) { bool byRef = argTree.AND() != null; if (byRef) yield return "&" + GetName(argTree); else yield return GetName(argTree); } }
private static string GetName(PhpParser.FunctionParameterContext context) { Contract.Requires(context != null); ITerminalNode nameNode = context.PHP_IDENTIFIER(); if (nameNode == null) return "?"; string name = nameNode.Symbol.Text; if (string.IsNullOrEmpty(name)) return "?"; return name; }
private static string GetQualifiedName(PhpParser.ClassOrInterfaceDefinitionContext context) { string name = GetName(context); for (RuleContext parent = context.Parent; parent != null; parent = parent.Parent) { var defContext = parent as PhpParser.ClassOrInterfaceDefinitionContext; if (defContext != null) { name = GetName(defContext) + "." + name; } } return name; }
public override void EnterFunctionDefinition(PhpParser.FunctionDefinitionContext context) { string name = GetName(context); IEnumerable<string> args = ProcessArguments(context.functionParameterList()); string sig = string.Format("{0}({1})", name, string.Join(", ", args)); IEditorNavigationType navigationType = _provider.EditorNavigationTypeRegistryService.GetEditorNavigationType(PredefinedEditorNavigationTypes.Members); var startToken = _antlrParseResultArgs.Tokens[context.SourceInterval.a]; var stopToken = _antlrParseResultArgs.Tokens[context.SourceInterval.b]; SnapshotSpan span = new SnapshotSpan(_snapshot, new Span(startToken.StartIndex, stopToken.StopIndex - startToken.StartIndex + 1)); SnapshotSpan seek = span; if (context.PHP_IDENTIFIER() != null) seek = new SnapshotSpan(_snapshot, new Span(context.PHP_IDENTIFIER().Symbol.StartIndex, 0)); StandardGlyphGroup glyphGroup = StandardGlyphGroup.GlyphGroupMethod; //StandardGlyphItem glyphItem = GetGlyphItemFromChildModifier(tree); StandardGlyphItem glyphItem = StandardGlyphItem.GlyphItemPublic; ImageSource glyph = _provider.GlyphService.GetGlyph(glyphGroup, glyphItem); NavigationTargetStyle style = NavigationTargetStyle.None; _navigationTargets.Add(new EditorNavigationTarget(sig, navigationType, span, seek, glyph, style)); }
public override void EnterClassOrInterfaceDefinition(PhpParser.ClassOrInterfaceDefinitionContext context) { string name = GetQualifiedName(context); IEditorNavigationType navigationType = _provider.EditorNavigationTypeRegistryService.GetEditorNavigationType(PredefinedEditorNavigationTypes.Types); var startToken = _antlrParseResultArgs.Tokens[context.SourceInterval.a]; var stopToken = _antlrParseResultArgs.Tokens[context.SourceInterval.b]; SnapshotSpan span = new SnapshotSpan(_snapshot, new Span(startToken.StartIndex, stopToken.StopIndex - startToken.StartIndex + 1)); SnapshotSpan seek = span; if (context.PHP_IDENTIFIER() != null) seek = new SnapshotSpan(_snapshot, new Span(context.PHP_IDENTIFIER().Symbol.StartIndex, 0)); StandardGlyphGroup glyphGroup; if (context.KW_INTERFACE() != null) { glyphGroup = StandardGlyphGroup.GlyphGroupInterface; } else { glyphGroup = StandardGlyphGroup.GlyphGroupClass; } //StandardGlyphItem glyphItem = GetGlyphItemFromChildModifier(child); StandardGlyphItem glyphItem = StandardGlyphItem.GlyphItemPublic; ImageSource glyph = _provider.GlyphService.GetGlyph(glyphGroup, glyphItem); NavigationTargetStyle style = NavigationTargetStyle.None; _navigationTargets.Add(new EditorNavigationTarget(name, navigationType, span, seek, glyph, style)); }