-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest.cs
More file actions
115 lines (94 loc) · 2.85 KB
/
test.cs
File metadata and controls
115 lines (94 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Nancy;
using Nancy.ModelBinding;
using Nancy.Hosting.Self;
using CommandLine;
using CommandLine.Text;
namespace roslyn_test_2
{
/* data-models */
public class Method
{
public String name { get; set; }
public String returnType { get; set; }
}
/* protocol-models */
public class ParseOne
{
public class Request
{
public String src { get; set; }
}
public class Response
{
public String className { get; set; }
public List<Method> methods { get; set; }
public Response()
{
methods = new List<Method>();
}
}
}
public class CodeAnalysis : NancyModule
{
public CodeAnalysis()
{
Post["/ca/parse_one"] = x => {
var request = this.Bind<ParseOne.Request>();
var response = new ParseOne.Response();
var tree = CSharpSyntaxTree.ParseText(request.src);
var methods =
tree.GetRoot().DescendantNodes().OfType<MethodDeclarationSyntax>();
var klass =
tree.GetRoot().DescendantNodes().OfType<ClassDeclarationSyntax>().First();
response.className = klass.Identifier.ValueText;
foreach (var method in methods)
{
response.methods.Add(new Method()
{
name = method.Identifier.ValueText,
returnType = method.ReturnType.ToString()
});
}
return Response.AsJson(response);
};
}
}
class Options
{
[Option('p', "port", DefaultValue = 9900,
HelpText = "port")]
public int port { get; set; }
[ParserState]
public IParserState LastParserState { get; set; }
[HelpOption]
public string GetUsage()
{
return HelpText.AutoBuild(this,
(HelpText current) => HelpText.DefaultParsingErrorsHandler(this, current));
}
}
class Program
{
static void Main(string[] args)
{
var options = new Options();
if (CommandLine.Parser.Default.ParseArguments(args, options) == false)
return;
var src = new StreamReader("../../src.txt").ReadToEnd();
using (var host = new NancyHost(new Uri("http://localhost:9900")))
{
host.Start();
Console.ReadLine();
}
}
}
}