1 | // Copyright (c) 2010-2013 AlphaSierraPapa for the SharpDevelop Team |
---|
2 | // |
---|
3 | // Permission is hereby granted, free of charge, to any person obtaining a copy of this |
---|
4 | // software and associated documentation files (the "Software"), to deal in the Software |
---|
5 | // without restriction, including without limitation the rights to use, copy, modify, merge, |
---|
6 | // publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons |
---|
7 | // to whom the Software is furnished to do so, subject to the following conditions: |
---|
8 | // |
---|
9 | // The above copyright notice and this permission notice shall be included in all copies or |
---|
10 | // substantial portions of the Software. |
---|
11 | // |
---|
12 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
---|
13 | // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR |
---|
14 | // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE |
---|
15 | // FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
---|
16 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
17 | // DEALINGS IN THE SOFTWARE. |
---|
18 | |
---|
19 | using System; |
---|
20 | using System.IO; |
---|
21 | |
---|
22 | namespace ICSharpCode.NRefactory.CSharp |
---|
23 | { |
---|
24 | public abstract class TokenWriter |
---|
25 | { |
---|
26 | public abstract void StartNode(AstNode node); |
---|
27 | public abstract void EndNode(AstNode node); |
---|
28 | |
---|
29 | /// <summary> |
---|
30 | /// Writes an identifier. |
---|
31 | /// </summary> |
---|
32 | public abstract void WriteIdentifier(Identifier identifier); |
---|
33 | |
---|
34 | /// <summary> |
---|
35 | /// Writes a keyword to the output. |
---|
36 | /// </summary> |
---|
37 | public abstract void WriteKeyword(Role role, string keyword); |
---|
38 | |
---|
39 | /// <summary> |
---|
40 | /// Writes a token to the output. |
---|
41 | /// </summary> |
---|
42 | public abstract void WriteToken(Role role, string token); |
---|
43 | |
---|
44 | /// <summary> |
---|
45 | /// Writes a primitive/literal value |
---|
46 | /// </summary> |
---|
47 | public abstract void WritePrimitiveValue(object value, string literalValue = null); |
---|
48 | |
---|
49 | public abstract void WritePrimitiveType(string type); |
---|
50 | |
---|
51 | public abstract void Space(); |
---|
52 | public abstract void Indent(); |
---|
53 | public abstract void Unindent(); |
---|
54 | public abstract void NewLine(); |
---|
55 | |
---|
56 | public abstract void WriteComment(CommentType commentType, string content); |
---|
57 | public abstract void WritePreProcessorDirective(PreProcessorDirectiveType type, string argument); |
---|
58 | |
---|
59 | public static TokenWriter Create(TextWriter writer, string indentation = "\t") |
---|
60 | { |
---|
61 | return new InsertSpecialsDecorator(new InsertRequiredSpacesDecorator(new TextWriterTokenWriter(writer) { IndentationString = indentation })); |
---|
62 | } |
---|
63 | |
---|
64 | public static TokenWriter CreateWriterThatSetsLocationsInAST(TextWriter writer, string indentation = "\t") |
---|
65 | { |
---|
66 | var target = new TextWriterTokenWriter(writer) { IndentationString = indentation }; |
---|
67 | return new InsertSpecialsDecorator(new InsertRequiredSpacesDecorator(new InsertMissingTokensDecorator(target, target))); |
---|
68 | } |
---|
69 | |
---|
70 | public static TokenWriter WrapInWriterThatSetsLocationsInAST(TokenWriter writer) |
---|
71 | { |
---|
72 | if (!(writer is ILocatable)) |
---|
73 | throw new InvalidOperationException("writer does not provide locations!"); |
---|
74 | return new InsertSpecialsDecorator(new InsertRequiredSpacesDecorator(new InsertMissingTokensDecorator(writer, (ILocatable)writer))); |
---|
75 | } |
---|
76 | } |
---|
77 | |
---|
78 | public interface ILocatable |
---|
79 | { |
---|
80 | TextLocation Location { get; } |
---|
81 | } |
---|
82 | |
---|
83 | public abstract class DecoratingTokenWriter : TokenWriter |
---|
84 | { |
---|
85 | TokenWriter decoratedWriter; |
---|
86 | |
---|
87 | protected DecoratingTokenWriter(TokenWriter decoratedWriter) |
---|
88 | { |
---|
89 | if (decoratedWriter == null) |
---|
90 | throw new ArgumentNullException("decoratedWriter"); |
---|
91 | this.decoratedWriter = decoratedWriter; |
---|
92 | } |
---|
93 | |
---|
94 | public override void StartNode(AstNode node) |
---|
95 | { |
---|
96 | decoratedWriter.StartNode(node); |
---|
97 | } |
---|
98 | |
---|
99 | public override void EndNode(AstNode node) |
---|
100 | { |
---|
101 | decoratedWriter.EndNode(node); |
---|
102 | } |
---|
103 | |
---|
104 | public override void WriteIdentifier(Identifier identifier) |
---|
105 | { |
---|
106 | decoratedWriter.WriteIdentifier(identifier); |
---|
107 | } |
---|
108 | |
---|
109 | public override void WriteKeyword(Role role, string keyword) |
---|
110 | { |
---|
111 | decoratedWriter.WriteKeyword(role, keyword); |
---|
112 | } |
---|
113 | |
---|
114 | public override void WriteToken(Role role, string token) |
---|
115 | { |
---|
116 | decoratedWriter.WriteToken(role, token); |
---|
117 | } |
---|
118 | |
---|
119 | public override void WritePrimitiveValue(object value, string literalValue = null) |
---|
120 | { |
---|
121 | decoratedWriter.WritePrimitiveValue(value, literalValue); |
---|
122 | } |
---|
123 | |
---|
124 | public override void WritePrimitiveType(string type) |
---|
125 | { |
---|
126 | decoratedWriter.WritePrimitiveType(type); |
---|
127 | } |
---|
128 | |
---|
129 | public override void Space() |
---|
130 | { |
---|
131 | decoratedWriter.Space(); |
---|
132 | } |
---|
133 | |
---|
134 | public override void Indent() |
---|
135 | { |
---|
136 | decoratedWriter.Indent(); |
---|
137 | } |
---|
138 | |
---|
139 | public override void Unindent() |
---|
140 | { |
---|
141 | decoratedWriter.Unindent(); |
---|
142 | } |
---|
143 | |
---|
144 | public override void NewLine() |
---|
145 | { |
---|
146 | decoratedWriter.NewLine(); |
---|
147 | } |
---|
148 | |
---|
149 | public override void WriteComment(CommentType commentType, string content) |
---|
150 | { |
---|
151 | decoratedWriter.WriteComment(commentType, content); |
---|
152 | } |
---|
153 | |
---|
154 | public override void WritePreProcessorDirective(PreProcessorDirectiveType type, string argument) |
---|
155 | { |
---|
156 | decoratedWriter.WritePreProcessorDirective(type, argument); |
---|
157 | } |
---|
158 | } |
---|
159 | } |
---|
160 | |
---|
161 | |
---|