From e4b64ada8f46b85f0f117e19b76dbeaa7c41be53 Mon Sep 17 00:00:00 2001 From: Manu Date: Fri, 1 Jul 2016 15:06:38 +0900 Subject: [PATCH] Fix invalid newlines --- Argument.cs | 120 +-- Feature.cs | 36 +- FileLexerSource.cs | 2 +- FixedTokenSource.cs | 110 +-- JavaFileSystem.cs | 40 +- LexerException.cs | 58 +- LexerSource.cs | 1530 +++++++++++++++++------------------ Macro.cs | 160 ++-- MacroTokenSource.cs | 78 +- Preprocessor.cs | 2 +- PreprocessorListener.cs | 2 +- PreprocessorListenerBase.cs | 2 +- Source.cs | 52 +- SourceIterator.cs | 38 +- State.cs | 32 +- StringLexerSource.cs | 2 +- Token.cs | 2 +- VirtualFile.cs | 2 +- VirtualFileSystem.cs | 36 +- Warning.cs | 2 +- 20 files changed, 1153 insertions(+), 1153 deletions(-) diff --git a/Argument.cs b/Argument.cs index 340ed77..16d8a36 100644 --- a/Argument.cs +++ b/Argument.cs @@ -1,72 +1,72 @@ -/* - * Anarres C Preprocessor - * Copyright (c) 2007-2008, Shevek - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express - * or implied. See the License for the specific language governing - * permissions and limitations under the License. - */ -using System; +/* + * Anarres C Preprocessor + * Copyright (c) 2007-2008, Shevek + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing + * permissions and limitations under the License. + */ +using System; using System.Collections.Generic; -using System.Text; - -namespace CppNet { -/** - * A macro argument. - * - * This encapsulates a raw and preprocessed token stream. - */ -internal class Argument : List { - public const int NO_ARGS = -1; - - private List _expansion; - +using System.Text; + +namespace CppNet { +/** + * A macro argument. + * + * This encapsulates a raw and preprocessed token stream. + */ +internal class Argument : List { + public const int NO_ARGS = -1; + + private List _expansion; + public Argument() { - this._expansion = null; - } - - public void addToken(Token tok) { - Add(tok); - } - - internal void expand(Preprocessor p) { + this._expansion = null; + } + + public void addToken(Token tok) { + Add(tok); + } + + internal void expand(Preprocessor p) { /* Cache expansion. */ if(_expansion == null) { - this._expansion = p.expand(this); - // System.out.println("Expanded arg " + this); - } + this._expansion = p.expand(this); + // System.out.println("Expanded arg " + this); + } } public Iterator expansion() { return _expansion.iterator(); - } - - override public String ToString() { - StringBuilder buf = new StringBuilder(); - buf.Append("Argument("); - // buf.Append(super.toString()); - buf.Append("raw=[ "); - for (int i = 0; i < this.Count; i++) - buf.Append(this[i].getText()); + } + + override public String ToString() { + StringBuilder buf = new StringBuilder(); + buf.Append("Argument("); + // buf.Append(super.toString()); + buf.Append("raw=[ "); + for (int i = 0; i < this.Count; i++) + buf.Append(this[i].getText()); buf.Append(" ];expansion=[ "); - if(_expansion == null) - buf.Append("null"); + if(_expansion == null) + buf.Append("null"); else for(int i = 0; i < _expansion.Count; i++) - buf.Append(_expansion[i].getText()); - buf.Append(" ])"); - return buf.ToString(); - } - -} - -} \ No newline at end of file + buf.Append(_expansion[i].getText()); + buf.Append(" ])"); + return buf.ToString(); + } + +} + +} diff --git a/Feature.cs b/Feature.cs index e66afad..c7fef4f 100644 --- a/Feature.cs +++ b/Feature.cs @@ -1,26 +1,26 @@ -/* - * Anarres C Preprocessor - * Copyright (c) 2007-2008, Shevek - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express - * or implied. See the License for the specific language governing - * permissions and limitations under the License. +/* + * Anarres C Preprocessor + * Copyright (c) 2007-2008, Shevek + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing + * permissions and limitations under the License. */ using System; namespace CppNet { - /** - * Features of the Preprocessor, which may be enabled or disabled. + /** + * Features of the Preprocessor, which may be enabled or disabled. */ [Flags] public enum Feature @@ -46,4 +46,4 @@ namespace CppNet INCLUDENEXT = 1 << 9 } -} \ No newline at end of file +} diff --git a/FileLexerSource.cs b/FileLexerSource.cs index 9ae9704..26d6f17 100644 --- a/FileLexerSource.cs +++ b/FileLexerSource.cs @@ -64,4 +64,4 @@ public class FileLexerSource : LexerSource { } } -} \ No newline at end of file +} diff --git a/FixedTokenSource.cs b/FixedTokenSource.cs index 3a2c076..396fc50 100644 --- a/FixedTokenSource.cs +++ b/FixedTokenSource.cs @@ -1,57 +1,57 @@ -/* - * Anarres C Preprocessor - * Copyright (c) 2007-2008, Shevek - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express - * or implied. See the License for the specific language governing - * permissions and limitations under the License. - */ - -using System; +/* + * Anarres C Preprocessor + * Copyright (c) 2007-2008, Shevek + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing + * permissions and limitations under the License. + */ + +using System; using System.Text; -using System.Collections.Generic; - -namespace CppNet { - -internal class FixedTokenSource : Source { - private static readonly Token EOF = - new Token(Token.EOF, ""); - - private List tokens; - private int idx; - +using System.Collections.Generic; + +namespace CppNet { + +internal class FixedTokenSource : Source { + private static readonly Token EOF = + new Token(Token.EOF, ""); + + private List tokens; + private int idx; + internal FixedTokenSource(params Token[] tokens) { - this.tokens = new List(tokens); - this.idx = 0; - } - - internal FixedTokenSource(List tokens) { - this.tokens = tokens; - this.idx = 0; - } - - public override Token token() { - if (idx >= tokens.Count) - return EOF; - return tokens[idx++]; - } - - override public String ToString() { - StringBuilder buf = new StringBuilder(); - buf.Append("constant token stream " + tokens); - Source parent = getParent(); - if (parent != null) - buf.Append(" in ").Append(parent); - return buf.ToString(); - } -} - -} \ No newline at end of file + this.tokens = new List(tokens); + this.idx = 0; + } + + internal FixedTokenSource(List tokens) { + this.tokens = tokens; + this.idx = 0; + } + + public override Token token() { + if (idx >= tokens.Count) + return EOF; + return tokens[idx++]; + } + + override public String ToString() { + StringBuilder buf = new StringBuilder(); + buf.Append("constant token stream " + tokens); + Source parent = getParent(); + if (parent != null) + buf.Append(" in ").Append(parent); + return buf.ToString(); + } +} + +} diff --git a/JavaFileSystem.cs b/JavaFileSystem.cs index 006683a..4198b4a 100644 --- a/JavaFileSystem.cs +++ b/JavaFileSystem.cs @@ -1,27 +1,27 @@ -/* - * Anarres C Preprocessor - * Copyright (c) 2007-2008, Shevek - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express - * or implied. See the License for the specific language governing - * permissions and limitations under the License. - */ -using System; +/* + * Anarres C Preprocessor + * Copyright (c) 2007-2008, Shevek + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing + * permissions and limitations under the License. + */ +using System; using System.IO; namespace CppNet { - /** - * A virtual filesystem implementation using java.io. + /** + * A virtual filesystem implementation using java.io. */ public class JavaFileSystem : VirtualFileSystem { @@ -37,4 +37,4 @@ namespace CppNet return new JavaFile(Path.Combine(dir, name)); } } -} \ No newline at end of file +} diff --git a/LexerException.cs b/LexerException.cs index c997bcc..1b11fdc 100644 --- a/LexerException.cs +++ b/LexerException.cs @@ -1,32 +1,32 @@ -/* - * Anarres C Preprocessor - * Copyright (c) 2007-2008, Shevek - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express - * or implied. See the License for the specific language governing - * permissions and limitations under the License. +/* + * Anarres C Preprocessor + * Copyright (c) 2007-2008, Shevek + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing + * permissions and limitations under the License. */ -using System; - -namespace CppNet { -/** - * A preprocessor exception. - * - * Note to users: I don't really like the name of this class. S. - */ +using System; + +namespace CppNet { +/** + * A preprocessor exception. + * + * Note to users: I don't really like the name of this class. S. + */ public class LexerException : Exception { - public LexerException(String msg) : base(msg) { } - - public LexerException(Exception cause) : base(cause.Message, cause) {} -} - -} \ No newline at end of file + public LexerException(String msg) : base(msg) { } + + public LexerException(Exception cause) : base(cause.Message, cause) {} +} + +} diff --git a/LexerSource.cs b/LexerSource.cs index ad1e4f3..336372c 100644 --- a/LexerSource.cs +++ b/LexerSource.cs @@ -1,40 +1,40 @@ -/* - * Anarres C Preprocessor - * Copyright (c) 2007-2008, Shevek - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express - * or implied. See the License for the specific language governing - * permissions and limitations under the License. +/* + * Anarres C Preprocessor + * Copyright (c) 2007-2008, Shevek + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing + * permissions and limitations under the License. */ using System; using System.IO; -using System.Text; -using System.Globalization; - -namespace CppNet { - -/** Does not handle digraphs. */ -public class LexerSource : Source { +using System.Text; +using System.Globalization; + +namespace CppNet { + +/** Does not handle digraphs. */ +public class LexerSource : Source { static bool isJavaIdentifierStart(int c) { - return char.IsLetter((char)c) || c == '$' || c == '_'; + return char.IsLetter((char)c) || c == '$' || c == '_'; } static bool isJavaIdentifierPart(int c) { return char.IsLetter((char)c) || c == '$' || c == '_' || char.IsDigit((char)c); - } - static bool isIdentifierIgnorable(int c) { - return c >= 0 && c <= 8 || - c >= 0xE && c <= 0x1B || - c >= 0x7F && c <= 0x9F || - CharUnicodeInfo.GetUnicodeCategory((char)c) == UnicodeCategory.Format; + } + static bool isIdentifierIgnorable(int c) { + return c >= 0 && c <= 8 || + c >= 0xE && c <= 0x1B || + c >= 0x7F && c <= 0x9F || + CharUnicodeInfo.GetUnicodeCategory((char)c) == UnicodeCategory.Format; } static int digit(char ch, int radix) @@ -60,757 +60,757 @@ public class LexerSource : Source { } return alphabet.IndexOf(ch); - } - private static readonly bool DEBUG = false; - - private JoinReader reader; - private bool ppvalid; - private bool bol; - private bool include; - - private bool digraphs; - - /* Unread. */ - private int u0, u1; - private int ucount; - - private int line; - private int column; - private int lastcolumn; - private bool cr; - - /* ppvalid is: - * false in StringLexerSource, - * true in FileLexerSource */ - public LexerSource(TextReader r, bool ppvalid) { - this.reader = new JoinReader(r); - this.ppvalid = ppvalid; - this.bol = true; - this.include = false; - - this.digraphs = true; - - this.ucount = 0; - - this.line = 1; - this.column = 0; - this.lastcolumn = -1; - this.cr = false; - } - - override internal void init(Preprocessor pp) { - base.init(pp); - this.digraphs = pp.getFeature(Feature.DIGRAPHS); - this.reader.init(pp, this); - } - - - override public int getLine() { - return line; - } - - - override public int getColumn() { - return column; - } - - - override internal bool isNumbered() { - return true; - } - -/* Error handling. */ - - private void _error(String msg, bool error) { - int _l = line; - int _c = column; - if (_c == 0) { - _c = lastcolumn; - _l--; - } - else { - _c--; - } - if (error) - base.error(_l, _c, msg); - else - base.warning(_l, _c, msg); - } - + } + private static readonly bool DEBUG = false; + + private JoinReader reader; + private bool ppvalid; + private bool bol; + private bool include; + + private bool digraphs; + + /* Unread. */ + private int u0, u1; + private int ucount; + + private int line; + private int column; + private int lastcolumn; + private bool cr; + + /* ppvalid is: + * false in StringLexerSource, + * true in FileLexerSource */ + public LexerSource(TextReader r, bool ppvalid) { + this.reader = new JoinReader(r); + this.ppvalid = ppvalid; + this.bol = true; + this.include = false; + + this.digraphs = true; + + this.ucount = 0; + + this.line = 1; + this.column = 0; + this.lastcolumn = -1; + this.cr = false; + } + + override internal void init(Preprocessor pp) { + base.init(pp); + this.digraphs = pp.getFeature(Feature.DIGRAPHS); + this.reader.init(pp, this); + } + + + override public int getLine() { + return line; + } + + + override public int getColumn() { + return column; + } + + + override internal bool isNumbered() { + return true; + } + +/* Error handling. */ + + private void _error(String msg, bool error) { + int _l = line; + int _c = column; + if (_c == 0) { + _c = lastcolumn; + _l--; + } + else { + _c--; + } + if (error) + base.error(_l, _c, msg); + else + base.warning(_l, _c, msg); + } + /* Allow JoinReader to call this. */ internal void error(String msg) - { - _error(msg, true); - } - - /* Allow JoinReader to call this. */ - internal void warning(String msg) { - _error(msg, false); - } - + { + _error(msg, true); + } + + /* Allow JoinReader to call this. */ + internal void warning(String msg) { + _error(msg, false); + } + /* A flag for string handling. */ internal void setInclude(bool b) - { - this.include = b; - } - -/* - private bool _isLineSeparator(int c) { - return Character.getType(c) == Character.LINE_SEPARATOR - || c == -1; - } -*/ - - /* XXX Move to JoinReader and canonicalise newlines. */ - private static bool isLineSeparator(int c) { - switch ((char)c) { - case '\r': - case '\n': - case '\u2028': - case '\u2029': - case '\u000B': - case '\u000C': - case '\u0085': - return true; - default: - return (c == -1); - } - } - - - private int read() { - System.Diagnostics.Debug.Assert(ucount <= 2, "Illegal ucount: " + ucount); - switch (ucount) { - case 2: - ucount = 1; - return u1; - case 1: - ucount = 0; - return u0; - } - - if (reader == null) - return -1; - - int c = reader.read(); - switch (c) { - case '\r': - cr = true; - line++; - lastcolumn = column; - column = 0; - break; - case '\n': - if (cr) { - cr = false; - break; + { + this.include = b; + } + +/* + private bool _isLineSeparator(int c) { + return Character.getType(c) == Character.LINE_SEPARATOR + || c == -1; + } +*/ + + /* XXX Move to JoinReader and canonicalise newlines. */ + private static bool isLineSeparator(int c) { + switch ((char)c) { + case '\r': + case '\n': + case '\u2028': + case '\u2029': + case '\u000B': + case '\u000C': + case '\u0085': + return true; + default: + return (c == -1); + } + } + + + private int read() { + System.Diagnostics.Debug.Assert(ucount <= 2, "Illegal ucount: " + ucount); + switch (ucount) { + case 2: + ucount = 1; + return u1; + case 1: + ucount = 0; + return u0; + } + + if (reader == null) + return -1; + + int c = reader.read(); + switch (c) { + case '\r': + cr = true; + line++; + lastcolumn = column; + column = 0; + break; + case '\n': + if (cr) { + cr = false; + break; } - goto case '\u2028'; - /* fallthrough */ - case '\u2028': - case '\u2029': - case '\u000B': - case '\u000C': - case '\u0085': - cr = false; - line++; - lastcolumn = column; - column = 0; - break; - default: - cr = false; - column++; - break; - } - -/* - if (isLineSeparator(c)) { - line++; - lastcolumn = column; - column = 0; - } - else { - column++; - } -*/ - - return c; - } - - /* You can unget AT MOST one newline. */ - private void unread(int c) { - /* XXX Must unread newlines. */ - if (c != -1) { - if (isLineSeparator(c)) { - line--; - column = lastcolumn; - cr = false; - } - else { - column--; - } - switch (ucount) { - case 0: - u0 = c; - ucount = 1; - break; - case 1: - u1 = c; - ucount = 2; - break; - default: - throw new InvalidOperationException( - "Cannot unget another character!" - ); - } - // reader.unread(c); - } - } - - /* Consumes the rest of the current line into an invalid. */ - private Token invalid(StringBuilder text, String reason) { - int d = read(); - while (!isLineSeparator(d)) { - text.Append((char)d); - d = read(); - } - unread(d); - return new Token(Token.INVALID, text.ToString(), reason); - } - - private Token ccomment() { - StringBuilder text = new StringBuilder("/*"); - int d; - do { - do { - d = read(); - text.Append((char)d); - } while (d != '*'); - do { - d = read(); - text.Append((char)d); - } while (d == '*'); - } while (d != '/'); - return new Token(Token.CCOMMENT, text.ToString()); - } - - private Token cppcomment() { - StringBuilder text = new StringBuilder("//"); - int d = read(); - while (!isLineSeparator(d)) { - text.Append((char)d); - d = read(); - } - unread(d); - return new Token(Token.CPPCOMMENT, text.ToString()); - } - - private int escape(StringBuilder text) { - int d = read(); - switch (d) { - case 'a': text.Append('a'); return 0x07; - case 'b': text.Append('b'); return '\b'; - case 'f': text.Append('f'); return '\f'; - case 'n': text.Append('n'); return '\n'; - case 'r': text.Append('r'); return '\r'; - case 't': text.Append('t'); return '\t'; - case 'v': text.Append('v'); return 0x0b; - case '\\': text.Append('\\'); return '\\'; - - case '0': case '1': case '2': case '3': - case '4': case '5': case '6': case '7': - int len = 0; - int val = 0; - do { - val = (val << 3) + digit((char)d, 8); - text.Append((char)d); - d = read(); - } while(++len < 3 && digit((char)d, 8) != -1); - unread(d); - return val; - - case 'x': - len = 0; - val = 0; - do { - val = (val << 4) + digit((char)d, 16); - text.Append((char)d); - d = read(); - } while(++len < 2 && digit((char)d, 16) != -1); - unread(d); - return val; - - /* Exclude two cases from the warning. */ - case '"': text.Append('"'); return '"'; - case '\'': text.Append('\''); return '\''; - - default: - warning("Unnecessary escape character " + (char)d); - text.Append((char)d); - return d; - } - } - - private Token character() { - StringBuilder text = new StringBuilder("'"); - int d = read(); - if (d == '\\') { - text.Append('\\'); - d = escape(text); - } - else if (isLineSeparator(d)) { - unread(d); - return new Token(Token.INVALID, text.ToString(), - "Unterminated character literal"); - } - else if (d == '\'') { - text.Append('\''); - return new Token(Token.INVALID, text.ToString(), - "Empty character literal"); - } - else if (char.IsControl((char)d)) { - text.Append('?'); - return invalid(text, "Illegal unicode character literal"); - } - else { - text.Append((char)d); - } - - int e = read(); - if (e != '\'') { - // error("Illegal character constant"); - /* We consume up to the next ' or the rest of the line. */ - for (;;) { - if (isLineSeparator(e)) { - unread(e); - break; - } - text.Append((char)e); - if (e == '\'') - break; - e = read(); + goto case '\u2028'; + /* fallthrough */ + case '\u2028': + case '\u2029': + case '\u000B': + case '\u000C': + case '\u0085': + cr = false; + line++; + lastcolumn = column; + column = 0; + break; + default: + cr = false; + column++; + break; + } + +/* + if (isLineSeparator(c)) { + line++; + lastcolumn = column; + column = 0; + } + else { + column++; + } +*/ + + return c; + } + + /* You can unget AT MOST one newline. */ + private void unread(int c) { + /* XXX Must unread newlines. */ + if (c != -1) { + if (isLineSeparator(c)) { + line--; + column = lastcolumn; + cr = false; } - return new Token(Token.INVALID, text.ToString(), - "Illegal character constant " + text); - } - text.Append('\''); + else { + column--; + } + switch (ucount) { + case 0: + u0 = c; + ucount = 1; + break; + case 1: + u1 = c; + ucount = 2; + break; + default: + throw new InvalidOperationException( + "Cannot unget another character!" + ); + } + // reader.unread(c); + } + } + + /* Consumes the rest of the current line into an invalid. */ + private Token invalid(StringBuilder text, String reason) { + int d = read(); + while (!isLineSeparator(d)) { + text.Append((char)d); + d = read(); + } + unread(d); + return new Token(Token.INVALID, text.ToString(), reason); + } + + private Token ccomment() { + StringBuilder text = new StringBuilder("/*"); + int d; + do { + do { + d = read(); + text.Append((char)d); + } while (d != '*'); + do { + d = read(); + text.Append((char)d); + } while (d == '*'); + } while (d != '/'); + return new Token(Token.CCOMMENT, text.ToString()); + } + + private Token cppcomment() { + StringBuilder text = new StringBuilder("//"); + int d = read(); + while (!isLineSeparator(d)) { + text.Append((char)d); + d = read(); + } + unread(d); + return new Token(Token.CPPCOMMENT, text.ToString()); + } + + private int escape(StringBuilder text) { + int d = read(); + switch (d) { + case 'a': text.Append('a'); return 0x07; + case 'b': text.Append('b'); return '\b'; + case 'f': text.Append('f'); return '\f'; + case 'n': text.Append('n'); return '\n'; + case 'r': text.Append('r'); return '\r'; + case 't': text.Append('t'); return '\t'; + case 'v': text.Append('v'); return 0x0b; + case '\\': text.Append('\\'); return '\\'; + + case '0': case '1': case '2': case '3': + case '4': case '5': case '6': case '7': + int len = 0; + int val = 0; + do { + val = (val << 3) + digit((char)d, 8); + text.Append((char)d); + d = read(); + } while(++len < 3 && digit((char)d, 8) != -1); + unread(d); + return val; + + case 'x': + len = 0; + val = 0; + do { + val = (val << 4) + digit((char)d, 16); + text.Append((char)d); + d = read(); + } while(++len < 2 && digit((char)d, 16) != -1); + unread(d); + return val; + + /* Exclude two cases from the warning. */ + case '"': text.Append('"'); return '"'; + case '\'': text.Append('\''); return '\''; + + default: + warning("Unnecessary escape character " + (char)d); + text.Append((char)d); + return d; + } + } + + private Token character() { + StringBuilder text = new StringBuilder("'"); + int d = read(); + if (d == '\\') { + text.Append('\\'); + d = escape(text); + } + else if (isLineSeparator(d)) { + unread(d); + return new Token(Token.INVALID, text.ToString(), + "Unterminated character literal"); + } + else if (d == '\'') { + text.Append('\''); + return new Token(Token.INVALID, text.ToString(), + "Empty character literal"); + } + else if (char.IsControl((char)d)) { + text.Append('?'); + return invalid(text, "Illegal unicode character literal"); + } + else { + text.Append((char)d); + } + + int e = read(); + if (e != '\'') { + // error("Illegal character constant"); + /* We consume up to the next ' or the rest of the line. */ + for (;;) { + if (isLineSeparator(e)) { + unread(e); + break; + } + text.Append((char)e); + if (e == '\'') + break; + e = read(); + } + return new Token(Token.INVALID, text.ToString(), + "Illegal character constant " + text); + } + text.Append('\''); /* XXX It this a bad cast? */ - return new Token(Token.CHARACTER, - text.ToString(), (char)d); - } - - private Token String(char open, char close) { - StringBuilder text = new StringBuilder(); - text.Append(open); - - StringBuilder buf = new StringBuilder(); - - for (;;) { - int c = read(); - if (c == close) { - break; - } - else if (c == '\\') { - text.Append('\\'); - if (!include) { - char d = (char)escape(text); - buf.Append(d); - } - } - else if (c == -1) { - unread(c); - // error("End of file in string literal after " + buf); - return new Token(Token.INVALID, text.ToString(), - "End of file in string literal after " + buf); - } - else if (isLineSeparator(c)) { - unread(c); + return new Token(Token.CHARACTER, + text.ToString(), (char)d); + } + + private Token String(char open, char close) { + StringBuilder text = new StringBuilder(); + text.Append(open); + + StringBuilder buf = new StringBuilder(); + + for (;;) { + int c = read(); + if (c == close) { + break; + } + else if (c == '\\') { + text.Append('\\'); + if (!include) { + char d = (char)escape(text); + buf.Append(d); + } + } + else if (c == -1) { + unread(c); + // error("End of file in string literal after " + buf); + return new Token(Token.INVALID, text.ToString(), + "End of file in string literal after " + buf); + } + else if (isLineSeparator(c)) { + unread(c); // error("Unterminated string literal after " + buf); - return new Token(Token.INVALID, text.ToString(), - "Unterminated string literal after " + buf); - } - else { - text.Append((char)c); - buf.Append((char)c); - } - } + return new Token(Token.INVALID, text.ToString(), + "Unterminated string literal after " + buf); + } + else { + text.Append((char)c); + buf.Append((char)c); + } + } text.Append(close); - return new Token(close == '>' ? Token.HEADER : Token.STRING, - text.ToString(), buf.ToString()); - } - - private Token _number(StringBuilder text, long val, int d) { - int bits = 0; - for (;;) { - /* XXX Error check duplicate bits. */ - if (d == 'U' || d == 'u') { - bits |= 1; - text.Append((char)d); - d = read(); - } - else if (d == 'L' || d == 'l') { - if ((bits & 4) != 0) - /* XXX warn */ ; - bits |= 2; - text.Append((char)d); - d = read(); - } - else if (d == 'I' || d == 'i') { - if ((bits & 2) != 0) - /* XXX warn */ ; - bits |= 4; - text.Append((char)d); - d = read(); - } - else if (char.IsLetter((char)d)) { + return new Token(close == '>' ? Token.HEADER : Token.STRING, + text.ToString(), buf.ToString()); + } + + private Token _number(StringBuilder text, long val, int d) { + int bits = 0; + for (;;) { + /* XXX Error check duplicate bits. */ + if (d == 'U' || d == 'u') { + bits |= 1; + text.Append((char)d); + d = read(); + } + else if (d == 'L' || d == 'l') { + if ((bits & 4) != 0) + /* XXX warn */ ; + bits |= 2; + text.Append((char)d); + d = read(); + } + else if (d == 'I' || d == 'i') { + if ((bits & 2) != 0) + /* XXX warn */ ; + bits |= 4; + text.Append((char)d); + d = read(); + } + else if (char.IsLetter((char)d)) { unread(d); - return new Token(Token.INVALID, text.ToString(), - "Invalid suffix \"" + (char)d + - "\" on numeric constant"); - } - else { + return new Token(Token.INVALID, text.ToString(), + "Invalid suffix \"" + (char)d + + "\" on numeric constant"); + } + else { unread(d); - return new Token(Token.INTEGER, - text.ToString(), (long)val); - } - } - } - - /* We already chewed a zero, so empty is fine. */ - private Token number_octal() { - StringBuilder text = new StringBuilder("0"); - int d = read(); - long val = 0; - while (digit((char)d, 8) != -1) { - val = (val << 3) + digit((char)d, 8); - text.Append((char)d); - d = read(); - } - return _number(text, val, d); - } - - /* We do not know whether know the first digit is valid. */ - private Token number_hex(char x) { - StringBuilder text = new StringBuilder("0"); - text.Append(x); - int d = read(); - if (digit((char)d, 16) == -1) { - unread(d); + return new Token(Token.INTEGER, + text.ToString(), (long)val); + } + } + } + + /* We already chewed a zero, so empty is fine. */ + private Token number_octal() { + StringBuilder text = new StringBuilder("0"); + int d = read(); + long val = 0; + while (digit((char)d, 8) != -1) { + val = (val << 3) + digit((char)d, 8); + text.Append((char)d); + d = read(); + } + return _number(text, val, d); + } + + /* We do not know whether know the first digit is valid. */ + private Token number_hex(char x) { + StringBuilder text = new StringBuilder("0"); + text.Append(x); + int d = read(); + if (digit((char)d, 16) == -1) { + unread(d); // error("Illegal hexadecimal constant " + (char)d); - return new Token(Token.INVALID, text.ToString(), - "Illegal hexadecimal digit " + (char)d + - " after "+ text); - } - long val = 0; - do { - val = (val << 4) + digit((char)d, 16); - text.Append((char)d); - d = read(); - } while (digit((char)d, 16) != -1); - return _number(text, val, d); - } - - /* We know we have at least one valid digit, but empty is not - * fine. */ - /* XXX This needs a complete rewrite. */ - private Token number_decimal(int c) { - StringBuilder text = new StringBuilder((char)c); - int d = c; - long val = 0; - do { - val = val * 10 + digit((char)d, 10); - text.Append((char)d); - d = read(); - } while (digit((char)d, 10) != -1); - return _number(text, val, d); - } - - private Token identifier(int c) { - StringBuilder text = new StringBuilder(); - int d; - text.Append((char)c); - for (;;) { - d = read(); - if (isIdentifierIgnorable(d)) - ; - else if (isJavaIdentifierPart(d)) - text.Append((char)d); - else - break; - } + return new Token(Token.INVALID, text.ToString(), + "Illegal hexadecimal digit " + (char)d + + " after "+ text); + } + long val = 0; + do { + val = (val << 4) + digit((char)d, 16); + text.Append((char)d); + d = read(); + } while (digit((char)d, 16) != -1); + return _number(text, val, d); + } + + /* We know we have at least one valid digit, but empty is not + * fine. */ + /* XXX This needs a complete rewrite. */ + private Token number_decimal(int c) { + StringBuilder text = new StringBuilder((char)c); + int d = c; + long val = 0; + do { + val = val * 10 + digit((char)d, 10); + text.Append((char)d); + d = read(); + } while (digit((char)d, 10) != -1); + return _number(text, val, d); + } + + private Token identifier(int c) { + StringBuilder text = new StringBuilder(); + int d; + text.Append((char)c); + for (;;) { + d = read(); + if (isIdentifierIgnorable(d)) + ; + else if (isJavaIdentifierPart(d)) + text.Append((char)d); + else + break; + } unread(d); - return new Token(Token.IDENTIFIER, text.ToString()); - } - - private Token whitespace(int c) { - StringBuilder text = new StringBuilder(); - int d; - text.Append((char)c); - for (;;) { - d = read(); - if (ppvalid && isLineSeparator(d)) /* XXX Ugly. */ - break; - if (char.IsWhiteSpace((char)d)) - text.Append((char)d); - else - break; - } + return new Token(Token.IDENTIFIER, text.ToString()); + } + + private Token whitespace(int c) { + StringBuilder text = new StringBuilder(); + int d; + text.Append((char)c); + for (;;) { + d = read(); + if (ppvalid && isLineSeparator(d)) /* XXX Ugly. */ + break; + if (char.IsWhiteSpace((char)d)) + text.Append((char)d); + else + break; + } unread(d); - return new Token(Token.WHITESPACE, text.ToString()); - } - - /* No token processed by cond() contains a newline. */ - private Token cond(char c, int yes, int no) { - int d = read(); - if (c == d) - return new Token(yes); - unread(d); - return new Token(no); - } - - public override Token token() { - Token tok = null; - - int _l = line; - int _c = column; - - int c = read(); - int d; - - switch (c) { - case '\n': - if (ppvalid) { - bol = true; + return new Token(Token.WHITESPACE, text.ToString()); + } + + /* No token processed by cond() contains a newline. */ + private Token cond(char c, int yes, int no) { + int d = read(); + if (c == d) + return new Token(yes); + unread(d); + return new Token(no); + } + + public override Token token() { + Token tok = null; + + int _l = line; + int _c = column; + + int c = read(); + int d; + + switch (c) { + case '\n': + if (ppvalid) { + bol = true; if (include) { - tok = new Token(Token.NL, _l, _c, "\n"); - } - else { - int nls = 0; - do { - nls++; - d = read(); - } while (d == '\n'); - unread(d); - char[] text = new char[nls]; - for (int i = 0; i < text.Length; i++) - text[i] = '\n'; - // Skip the bol = false below. - tok = new Token(Token.NL, _l, _c, new String(text)); - } - if (DEBUG) - System.Console.Error.WriteLine("lx: Returning NL: " + tok); - return tok; - } - /* Let it be handled as whitespace. */ - break; - - case '!': - tok = cond('=', Token.NE, '!'); - break; - - case '#': - if (bol) - tok = new Token(Token.HASH); - else - tok = cond('#', Token.PASTE, '#'); - break; - - case '+': - d = read(); - if (d == '+') - tok = new Token(Token.INC); - else if (d == '=') - tok = new Token(Token.PLUS_EQ); - else - unread(d); - break; - case '-': - d = read(); - if (d == '-') - tok = new Token(Token.DEC); - else if (d == '=') - tok = new Token(Token.SUB_EQ); - else if (d == '>') - tok = new Token(Token.ARROW); - else - unread(d); - break; - - case '*': - tok = cond('=', Token.MULT_EQ, '*'); - break; - case '/': - d = read(); - if (d == '*') - tok = ccomment(); - else if (d == '/') - tok = cppcomment(); - else if (d == '=') - tok = new Token(Token.DIV_EQ); - else - unread(d); - break; - - case '%': - d = read(); - if (d == '=') - tok = new Token(Token.MOD_EQ); - else if (digraphs && d == '>') - tok = new Token('}'); // digraph - else if (digraphs && d == ':') { - bool paste = true; - d = read(); - if (d != '%') { - unread(d); - tok = new Token('#'); // digraph - paste = false; - } - d = read(); - if (d != ':') { - unread(d); // Unread 2 chars here. - unread('%'); - tok = new Token('#'); // digraph - paste = false; - } - if(paste) { - tok = new Token(Token.PASTE); // digraph - } - } - else - unread(d); - break; - - case ':': - /* :: */ - d = read(); - if (digraphs && d == '>') - tok = new Token(']'); // digraph - else - unread(d); - break; - - case '<': - if (include) { - tok = String('<', '>'); - } - else { - d = read(); - if (d == '=') - tok = new Token(Token.LE); - else if (d == '<') - tok = cond('=', Token.LSH_EQ, Token.LSH); - else if (digraphs && d == ':') - tok = new Token('['); // digraph - else if (digraphs && d == '%') - tok = new Token('{'); // digraph - else - unread(d); - } - break; - - case '=': - tok = cond('=', Token.EQ, '='); - break; - - case '>': - d = read(); - if (d == '=') - tok = new Token(Token.GE); - else if (d == '>') - tok = cond('=', Token.RSH_EQ, Token.RSH); - else - unread(d); - break; - - case '^': - tok = cond('=', Token.XOR_EQ, '^'); - break; - - case '|': - d = read(); - if (d == '=') - tok = new Token(Token.OR_EQ); - else if (d == '|') - tok = cond('=', Token.LOR_EQ, Token.LOR); - else - unread(d); - break; - case '&': - d = read(); - if (d == '&') - tok = cond('=', Token.LAND_EQ, Token.LAND); + tok = new Token(Token.NL, _l, _c, "\n"); + } + else { + int nls = 0; + do { + nls++; + d = read(); + } while (d == '\n'); + unread(d); + char[] text = new char[nls]; + for (int i = 0; i < text.Length; i++) + text[i] = '\n'; + // Skip the bol = false below. + tok = new Token(Token.NL, _l, _c, new String(text)); + } + if (DEBUG) + System.Console.Error.WriteLine("lx: Returning NL: " + tok); + return tok; + } + /* Let it be handled as whitespace. */ + break; + + case '!': + tok = cond('=', Token.NE, '!'); + break; + + case '#': + if (bol) + tok = new Token(Token.HASH); + else + tok = cond('#', Token.PASTE, '#'); + break; + + case '+': + d = read(); + if (d == '+') + tok = new Token(Token.INC); else if (d == '=') - tok = new Token(Token.AND_EQ); - else - unread(d); - break; - - case '.': - d = read(); + tok = new Token(Token.PLUS_EQ); + else + unread(d); + break; + case '-': + d = read(); + if (d == '-') + tok = new Token(Token.DEC); + else if (d == '=') + tok = new Token(Token.SUB_EQ); + else if (d == '>') + tok = new Token(Token.ARROW); + else + unread(d); + break; + + case '*': + tok = cond('=', Token.MULT_EQ, '*'); + break; + case '/': + d = read(); + if (d == '*') + tok = ccomment(); + else if (d == '/') + tok = cppcomment(); + else if (d == '=') + tok = new Token(Token.DIV_EQ); + else + unread(d); + break; + + case '%': + d = read(); + if (d == '=') + tok = new Token(Token.MOD_EQ); + else if (digraphs && d == '>') + tok = new Token('}'); // digraph + else if (digraphs && d == ':') { + bool paste = true; + d = read(); + if (d != '%') { + unread(d); + tok = new Token('#'); // digraph + paste = false; + } + d = read(); + if (d != ':') { + unread(d); // Unread 2 chars here. + unread('%'); + tok = new Token('#'); // digraph + paste = false; + } + if(paste) { + tok = new Token(Token.PASTE); // digraph + } + } + else + unread(d); + break; + + case ':': + /* :: */ + d = read(); + if (digraphs && d == '>') + tok = new Token(']'); // digraph + else + unread(d); + break; + + case '<': + if (include) { + tok = String('<', '>'); + } + else { + d = read(); + if (d == '=') + tok = new Token(Token.LE); + else if (d == '<') + tok = cond('=', Token.LSH_EQ, Token.LSH); + else if (digraphs && d == ':') + tok = new Token('['); // digraph + else if (digraphs && d == '%') + tok = new Token('{'); // digraph + else + unread(d); + } + break; + + case '=': + tok = cond('=', Token.EQ, '='); + break; + + case '>': + d = read(); + if (d == '=') + tok = new Token(Token.GE); + else if (d == '>') + tok = cond('=', Token.RSH_EQ, Token.RSH); + else + unread(d); + break; + + case '^': + tok = cond('=', Token.XOR_EQ, '^'); + break; + + case '|': + d = read(); + if (d == '=') + tok = new Token(Token.OR_EQ); + else if (d == '|') + tok = cond('=', Token.LOR_EQ, Token.LOR); + else + unread(d); + break; + case '&': + d = read(); + if (d == '&') + tok = cond('=', Token.LAND_EQ, Token.LAND); + else if (d == '=') + tok = new Token(Token.AND_EQ); + else + unread(d); + break; + + case '.': + d = read(); if (d == '.') - tok = cond('.', Token.ELLIPSIS, Token.RANGE); - else - unread(d); - /* XXX decimal fraction */ - break; - - case '0': - /* octal or hex */ - d = read(); - if (d == 'x' || d == 'X') - tok = number_hex((char)d); - else { - unread(d); - tok = number_octal(); - } - break; - - case '\'': - tok = character(); - break; - - case '"': - tok = String('"', '"'); - break; - - case -1: - close(); - tok = new Token(Token.EOF, _l, _c, ""); - break; - } - - if (tok == null) { - if (char.IsWhiteSpace((char)c)) { - tok = whitespace(c); - } - else if (char.IsDigit((char)c)) { - tok = number_decimal(c); - } - else if (isJavaIdentifierStart(c)) { - tok = identifier(c); - } - else { - tok = new Token(c); - } - } - - if (bol) { - switch (tok.getType()) { + tok = cond('.', Token.ELLIPSIS, Token.RANGE); + else + unread(d); + /* XXX decimal fraction */ + break; + + case '0': + /* octal or hex */ + d = read(); + if (d == 'x' || d == 'X') + tok = number_hex((char)d); + else { + unread(d); + tok = number_octal(); + } + break; + + case '\'': + tok = character(); + break; + + case '"': + tok = String('"', '"'); + break; + + case -1: + close(); + tok = new Token(Token.EOF, _l, _c, ""); + break; + } + + if (tok == null) { + if (char.IsWhiteSpace((char)c)) { + tok = whitespace(c); + } + else if (char.IsDigit((char)c)) { + tok = number_decimal(c); + } + else if (isJavaIdentifierStart(c)) { + tok = identifier(c); + } + else { + tok = new Token(c); + } + } + + if (bol) { + switch (tok.getType()) { case Token.WHITESPACE: - case Token.CCOMMENT: - break; - default: - bol = false; - break; - } - } - - tok.setLocation(_l, _c); - if (DEBUG) - System.Console.WriteLine("lx: Returning " + tok); - // (new Exception("here")).printStackTrace(System.out); - return tok; + case Token.CCOMMENT: + break; + default: + bol = false; + break; + } + } + + tok.setLocation(_l, _c); + if (DEBUG) + System.Console.WriteLine("lx: Returning " + tok); + // (new Exception("here")).printStackTrace(System.out); + return tok; } public override void close() @@ -820,7 +820,7 @@ public class LexerSource : Source { reader = null; } base.close(); - } -} - -} + } +} + +} diff --git a/Macro.cs b/Macro.cs index f4b0765..cefd6a1 100644 --- a/Macro.cs +++ b/Macro.cs @@ -1,18 +1,18 @@ -/* - * Anarres C Preprocessor - * Copyright (c) 2007-2008, Shevek - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express - * or implied. See the License for the specific language governing - * permissions and limitations under the License. +/* + * Anarres C Preprocessor + * Copyright (c) 2007-2008, Shevek + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing + * permissions and limitations under the License. */ using System; using System.Collections.Generic; @@ -20,20 +20,20 @@ using System.Text; namespace CppNet { - /** - * A macro object. - * - * This encapsulates a name, an argument count, and a token stream - * for replacement. The replacement token stream may contain the - * extra tokens {@link Token#M_ARG} and {@link Token#M_STRING}. + /** + * A macro object. + * + * This encapsulates a name, an argument count, and a token stream + * for replacement. The replacement token stream may contain the + * extra tokens {@link Token#M_ARG} and {@link Token#M_STRING}. */ public class Macro { private Source source; private String name; - /* It's an explicit decision to keep these around here. We don't - * need to; the argument token type is M_ARG and the value - * is the index. The strings themselves are only used in + /* It's an explicit decision to keep these around here. We don't + * need to; the argument token type is M_ARG and the value + * is the index. The strings themselves are only used in * stringification of the macro, for debugging. */ private List args; private bool variadic; @@ -53,96 +53,96 @@ namespace CppNet { } - /** - * Sets the Source from which this macro was parsed. + /** + * Sets the Source from which this macro was parsed. */ public void setSource(Source s) { this.source = s; } - /** - * Returns the Source from which this macro was parsed. - * - * This method may return null if the macro was not parsed - * from a regular file. + /** + * Returns the Source from which this macro was parsed. + * + * This method may return null if the macro was not parsed + * from a regular file. */ public Source getSource() { return source; } - /** - * Returns the name of this macro. + /** + * Returns the name of this macro. */ public String getName() { return name; } - /** - * Sets the arguments to this macro. + /** + * Sets the arguments to this macro. */ public void setArgs(List args) { this.args = args; } - /** - * Returns true if this is a function-like macro. + /** + * Returns true if this is a function-like macro. */ public bool isFunctionLike() { return args != null; } - /** - * Returns the number of arguments to this macro. + /** + * Returns the number of arguments to this macro. */ public int getArgs() { return args.Count; } - /** - * Sets the variadic flag on this Macro. + /** + * Sets the variadic flag on this Macro. */ public void setVariadic(bool b) { this.variadic = b; } - /** - * Returns true if this is a variadic function-like macro. + /** + * Returns true if this is a variadic function-like macro. */ public bool isVariadic() { return variadic; } - /** - * Adds a token to the expansion of this macro. + /** + * Adds a token to the expansion of this macro. */ public void addToken(Token tok) { this.tokens.Add(tok); } - /** - * Adds a "paste" operator to the expansion of this macro. - * - * A paste operator causes the next token added to be pasted - * to the previous token when the macro is expanded. - * It is an error for a macro to end with a paste token. + /** + * Adds a "paste" operator to the expansion of this macro. + * + * A paste operator causes the next token added to be pasted + * to the previous token when the macro is expanded. + * It is an error for a macro to end with a paste token. */ public void addPaste(Token tok) { - /* - * Given: tok0 ## tok1 - * We generate: M_PASTE, tok0, tok1 - * This extends as per a stack language: - * tok0 ## tok1 ## tok2 -> - * M_PASTE, tok0, M_PASTE, tok1, tok2 + /* + * Given: tok0 ## tok1 + * We generate: M_PASTE, tok0, tok1 + * This extends as per a stack language: + * tok0 ## tok1 ## tok2 -> + * M_PASTE, tok0, M_PASTE, tok1, tok2 */ this.tokens.Insert(tokens.Count - 1, tok); } @@ -152,30 +152,30 @@ namespace CppNet return tokens; } - /* Paste tokens are inserted before the first of the two pasted - * tokens, so it's a kind of bytecode notation. This method - * swaps them around again. We know that there will never be two + /* Paste tokens are inserted before the first of the two pasted + * tokens, so it's a kind of bytecode notation. This method + * swaps them around again. We know that there will never be two * sequential paste tokens, so a bool is sufficient. */ - public String getText() { - StringBuilder buf = new StringBuilder(); - bool paste = false; - for (int i = 0; i < tokens.Count; i++) { - Token tok = tokens[i]; - if (tok.getType() == Token.M_PASTE) { - System.Diagnostics.Debug.Assert(paste == false, "Two sequential pastes."); - paste = true; - continue; - } - else { - buf.Append(tok.getText()); - } - if (paste) { - buf.Append(" #" + "# "); - paste = false; - } - // buf.Append(tokens.get(i)); - } - return buf.ToString(); + public String getText() { + StringBuilder buf = new StringBuilder(); + bool paste = false; + for (int i = 0; i < tokens.Count; i++) { + Token tok = tokens[i]; + if (tok.getType() == Token.M_PASTE) { + System.Diagnostics.Debug.Assert(paste == false, "Two sequential pastes."); + paste = true; + continue; + } + else { + buf.Append(tok.getText()); + } + if (paste) { + buf.Append(" #" + "# "); + paste = false; + } + // buf.Append(tokens.get(i)); + } + return buf.ToString(); } override public String ToString() diff --git a/MacroTokenSource.cs b/MacroTokenSource.cs index c2d9413..6380d02 100644 --- a/MacroTokenSource.cs +++ b/MacroTokenSource.cs @@ -1,30 +1,30 @@ -/* - * Anarres C Preprocessor - * Copyright (c) 2007-2008, Shevek - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express - * or implied. See the License for the specific language governing - * permissions and limitations under the License. - */ -using System; -using System.Text; -using System.Collections.Generic; +/* + * Anarres C Preprocessor + * Copyright (c) 2007-2008, Shevek + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing + * permissions and limitations under the License. + */ +using System; +using System.Text; +using System.Collections.Generic; using boolean = System.Boolean; -using Debug = System.Diagnostics.Debug; - -namespace CppNet { - -/* This source should always be active, since we don't expand macros - * in any inactive context. */ -internal class MacroTokenSource : Source { +using Debug = System.Diagnostics.Debug; + +namespace CppNet { + +/* This source should always be active, since we don't expand macros + * in any inactive context. */ +internal class MacroTokenSource : Source { private Macro macro; private Iterator tokens; /* Pointer into the macro. */ private List args; /* { unexpanded, expanded } */ @@ -186,16 +186,16 @@ internal class MacroTokenSource : Source { } } /* for */ } - - - override public String ToString() { - StringBuilder buf = new StringBuilder(); - buf.Append("expansion of ").Append(macro.getName()); - Source parent = getParent(); - if (parent != null) - buf.Append(" in ").Append(parent); - return buf.ToString(); - } -} - -} \ No newline at end of file + + + override public String ToString() { + StringBuilder buf = new StringBuilder(); + buf.Append("expansion of ").Append(macro.getName()); + Source parent = getParent(); + if (parent != null) + buf.Append(" in ").Append(parent); + return buf.ToString(); + } +} + +} diff --git a/Preprocessor.cs b/Preprocessor.cs index d7ce287..c630393 100644 --- a/Preprocessor.cs +++ b/Preprocessor.cs @@ -2245,4 +2245,4 @@ public class Preprocessor : IDisposable { } -} \ No newline at end of file +} diff --git a/PreprocessorListener.cs b/PreprocessorListener.cs index 7bfdad2..dc3f0d7 100644 --- a/PreprocessorListener.cs +++ b/PreprocessorListener.cs @@ -49,4 +49,4 @@ namespace CppNet print(source.getName() + ":" + line + ":" + column + ": error: " + msg); } } -} \ No newline at end of file +} diff --git a/PreprocessorListenerBase.cs b/PreprocessorListenerBase.cs index 9d5472f..f0127c6 100644 --- a/PreprocessorListenerBase.cs +++ b/PreprocessorListenerBase.cs @@ -76,4 +76,4 @@ namespace CppNet public virtual void handleSourceChange(Source source, String ev) { } } -} \ No newline at end of file +} diff --git a/Source.cs b/Source.cs index 25ad911..168cebe 100644 --- a/Source.cs +++ b/Source.cs @@ -1,18 +1,18 @@ -/* - * Anarres C Preprocessor - * Copyright (c) 2007-2008, Shevek - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express - * or implied. See the License for the specific language governing - * permissions and limitations under the License. +/* + * Anarres C Preprocessor + * Copyright (c) 2007-2008, Shevek + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing + * permissions and limitations under the License. */ using System; using System.Collections.Generic; @@ -23,16 +23,16 @@ namespace CppNet { - /** - * An input to the Preprocessor. - * - * Inputs may come from Files, Strings or other sources. The - * preprocessor maintains a stack of Sources. Operations such as - * file inclusion or token pasting will push a new source onto - * the Preprocessor stack. Sources pop from the stack when they - * are exhausted; this may be transparent or explicit. - * - * BUG: Error messages are not handled properly. + /** + * An input to the Preprocessor. + * + * Inputs may come from Files, Strings or other sources. The + * preprocessor maintains a stack of Sources. Operations such as + * file inclusion or token pasting will push a new source onto + * the Preprocessor stack. Sources pop from the stack when they + * are exhausted; this may be transparent or explicit. + * + * BUG: Error messages are not handled properly. */ public abstract class Source : Iterable, Closeable { @@ -295,4 +295,4 @@ namespace CppNet } } -} \ No newline at end of file +} diff --git a/SourceIterator.cs b/SourceIterator.cs index caded34..a83b05a 100644 --- a/SourceIterator.cs +++ b/SourceIterator.cs @@ -1,18 +1,18 @@ -/* - * Anarres C Preprocessor - * Copyright (c) 2007-2008, Shevek - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express - * or implied. See the License for the specific language governing - * permissions and limitations under the License. +/* + * Anarres C Preprocessor + * Copyright (c) 2007-2008, Shevek + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing + * permissions and limitations under the License. */ using System; @@ -24,9 +24,9 @@ using boolean = System.Boolean; namespace CppNet { - /** - * An Iterator for {@link Source Sources}, - * returning {@link Token Tokens}. + /** + * An Iterator for {@link Source Sources}, + * returning {@link Token Tokens}. */ public class SourceIterator : Iterator { @@ -95,4 +95,4 @@ namespace CppNet } -} +} diff --git a/State.cs b/State.cs index 324cc78..60cf008 100644 --- a/State.cs +++ b/State.cs @@ -1,18 +1,18 @@ -/* - * Anarres C Preprocessor - * Copyright (c) 2007-2008, Shevek - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express - * or implied. See the License for the specific language governing - * permissions and limitations under the License. +/* + * Anarres C Preprocessor + * Copyright (c) 2007-2008, Shevek + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing + * permissions and limitations under the License. */ using System; @@ -86,4 +86,4 @@ namespace CppNet ", sawelse=" + _sawElse; } } -} \ No newline at end of file +} diff --git a/StringLexerSource.cs b/StringLexerSource.cs index d26e1ba..3ac13e5 100644 --- a/StringLexerSource.cs +++ b/StringLexerSource.cs @@ -66,4 +66,4 @@ public class StringLexerSource : LexerSource } } -} \ No newline at end of file +} diff --git a/Token.cs b/Token.cs index 6ecf707..8950ce4 100644 --- a/Token.cs +++ b/Token.cs @@ -350,4 +350,4 @@ namespace CppNet { } } -} \ No newline at end of file +} diff --git a/VirtualFile.cs b/VirtualFile.cs index 2995a02..58bbacd 100644 --- a/VirtualFile.cs +++ b/VirtualFile.cs @@ -30,4 +30,4 @@ public interface VirtualFile { Source getSource(); } -} \ No newline at end of file +} diff --git a/VirtualFileSystem.cs b/VirtualFileSystem.cs index e2eadd3..6456fd8 100644 --- a/VirtualFileSystem.cs +++ b/VirtualFileSystem.cs @@ -1,25 +1,25 @@ -/* - * Anarres C Preprocessor - * Copyright (c) 2007-2008, Shevek - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express - * or implied. See the License for the specific language governing - * permissions and limitations under the License. +/* + * Anarres C Preprocessor + * Copyright (c) 2007-2008, Shevek + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing + * permissions and limitations under the License. */ using System; namespace CppNet { - /** - * An extremely lightweight virtual file system interface. + /** + * An extremely lightweight virtual file system interface. */ public interface VirtualFileSystem { @@ -27,4 +27,4 @@ namespace CppNet VirtualFile getFile(String dir, String name); } -} \ No newline at end of file +} diff --git a/Warning.cs b/Warning.cs index 8d70132..3f83cd4 100644 --- a/Warning.cs +++ b/Warning.cs @@ -35,4 +35,4 @@ namespace CppNet // SYSTEM_HEADERS } -} \ No newline at end of file +}