5
// Levi Bard <levi@unity3d.com>
7
// Copyright (c) 2010 Levi Bard
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
10
// of this software and associated documentation files (the "Software"), to deal
11
// in the Software without restriction, including without limitation the rights
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
// copies of the Software, and to permit persons to whom the Software is
14
// furnished to do so, subject to the following conditions:
16
// The above copyright notice and this permission notice shall be included in
17
// all copies or substantial portions of the Software.
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
29
using System.Text.RegularExpressions;
30
using System.Collections.Generic;
32
using MonoDevelop.Core;
33
using MonoDevelop.Core.Execution;
35
namespace CBinding.Parser
37
public class BsdCTagsManager: CTagsManager
39
#region implemented abstract members of CBinding.Parser.CTagsManager
41
protected override IEnumerable<string> GetTags (FileInformation fileInfo)
43
string tagFileName = Path.GetFileName (fileInfo.FileName) + ".tag";
44
string tagFullFileName = Path.Combine (SystemTagsDirectory, tagFileName);
46
string ctags_options = string.Format ("-dtx '{0}'", fileInfo.FileName);
47
string ctags_output = string.Empty;
49
if (!File.Exists (tagFullFileName) || File.GetLastWriteTimeUtc (tagFullFileName) < File.GetLastWriteTimeUtc (fileInfo.FileName)) {
50
ctags_output = GetOutputFromProcess (CTagsExecutable, ctags_options, Environment.CurrentDirectory);
51
File.WriteAllText (tagFullFileName, ctags_output);
54
return ctags_output.Split (newlines, StringSplitOptions.RemoveEmptyEntries);
57
static readonly char[] newlines = {'\r','\n'};
58
protected override IEnumerable<string> GetTags (MonoDevelop.Projects.Project project, string filename, IEnumerable<string> headers)
60
StringBuilder ctags_kinds = new StringBuilder ("-dtx");
62
ctags_kinds.AppendFormat (" '{0}'", filename);
63
foreach (string header in headers) {
64
ctags_kinds.AppendFormat (" '{0}'", header);
67
string output = GetOutputFromProcess (CTagsExecutable, ctags_kinds.ToString (), project.BaseDirectory);
69
return output.Split (newlines, StringSplitOptions.RemoveEmptyEntries);
73
public override void FillFileInformation (FileInformation fileInfo)
75
IEnumerable<string> ctags_output = GetTags (fileInfo);
76
if (ctags_output == null) return;
78
foreach (string tagEntry in ctags_output) {
79
if (tagEntry.StartsWith ("!_")) continue;
81
Tag tag = ParseTag (tagEntry);
84
AddInfo (fileInfo, tag, tagEntry);
87
fileInfo.IsFilled = true;
91
// Format: symbol line file fulltext (there may not be any whitespace between symbol and line)
92
static readonly Regex tagExpression = new Regex (@"\s*(?<symbol>[^\s]+?)\s*(?<line>\d+)\s+(?<file>[^\s]+)\s+(?<raw>.*)", RegexOptions.Compiled);
94
public override Tag ParseTag (string tagEntry)
97
Match tagMatch = tagExpression.Match (tagEntry);
98
if (tagMatch == null) return null;
100
TagKind kind = TagKind.Member;
101
string signature = tagMatch.Groups["raw"].Value;
102
int start = signature.IndexOf ('(');
103
int end = signature.LastIndexOf (')');
105
if (start >= 0 && end > start) {
106
// Attempt to parse out method parameter block
107
signature = signature.Substring (start, end - start + 1);
108
kind = TagKind.Function; // TODO: improve kind guessing
110
return new Tag (tagMatch.Groups["symbol"].Value,
111
tagMatch.Groups["file"].Value,
112
ulong.Parse (tagMatch.Groups["line"].Value)+1,
113
kind, AccessModifier.Public,
114
null, null, null, null, null, signature);
115
} catch (Exception ex) {
116
LoggingService.LogWarning (string.Format ("Error parsing tag {0}", tagEntry), ex);