|
| 1 | +using System; |
| 2 | +using Winterdom.Viasfora.Rainbow; |
| 3 | +using Winterdom.Viasfora.Util; |
| 4 | + |
| 5 | +namespace Winterdom.Viasfora.Languages.BraceScanners { |
| 6 | + public class USqlBraceScanner : IBraceScanner { |
| 7 | + const int stText = 0; |
| 8 | + const int stString = 1; |
| 9 | + const int stMultiLineComment = 4; |
| 10 | + private int status = stText; |
| 11 | + |
| 12 | + public string BraceList { |
| 13 | + get { return "()[]{}"; } |
| 14 | + } |
| 15 | + |
| 16 | + public void Reset(int state) { |
| 17 | + status = stText; |
| 18 | + } |
| 19 | + public bool Extract(ITextChars tc, ref CharPos pos) { |
| 20 | + pos = CharPos.Empty; |
| 21 | + while ( !tc.EndOfLine ) { |
| 22 | + switch ( status ) { |
| 23 | + case stString: ParseString(tc); break; |
| 24 | + case stMultiLineComment: ParseMultilineComment(tc); break; |
| 25 | + default: |
| 26 | + return ParseText(tc, ref pos); |
| 27 | + } |
| 28 | + } |
| 29 | + return false; |
| 30 | + } |
| 31 | + |
| 32 | + private bool ParseText(ITextChars tc, ref CharPos pos) { |
| 33 | + while ( !tc.EndOfLine ) { |
| 34 | + if ( tc.Char() == '/' && tc.NChar() == '*' ) { |
| 35 | + tc.Skip(2); |
| 36 | + status = stMultiLineComment; |
| 37 | + ParseMultilineComment(tc); |
| 38 | + } else if ( tc.Char() == '/' && tc.NChar() == '/' ) { |
| 39 | + tc.SkipRemainder(); |
| 40 | + } else if ( tc.Char() == '\'' ) { |
| 41 | + status = stString; |
| 42 | + tc.Next(); |
| 43 | + ParseCharLiteral(tc); |
| 44 | + } else if ( tc.Char() == '"' ) { |
| 45 | + status = stString; |
| 46 | + tc.Next(); |
| 47 | + ParseString(tc); |
| 48 | + } else if ( this.BraceList.IndexOf(tc.Char()) >= 0 ) { |
| 49 | + pos = new CharPos(tc.Char(), tc.AbsolutePosition); |
| 50 | + tc.Next(); |
| 51 | + return true; |
| 52 | + } else { |
| 53 | + tc.Next(); |
| 54 | + } |
| 55 | + } |
| 56 | + return false; |
| 57 | + } |
| 58 | + |
| 59 | + private void ParseCharLiteral(ITextChars tc) { |
| 60 | + while ( !tc.EndOfLine ) { |
| 61 | + if ( tc.Char() == '\\' ) { |
| 62 | + // skip over escape sequences |
| 63 | + tc.Skip(2); |
| 64 | + } else if ( tc.Char() == '\'' ) { |
| 65 | + tc.Next(); |
| 66 | + break; |
| 67 | + } else { |
| 68 | + tc.Next(); |
| 69 | + } |
| 70 | + } |
| 71 | + this.status = stText; |
| 72 | + } |
| 73 | + |
| 74 | + private void ParseMultilineComment(ITextChars tc) { |
| 75 | + while ( !tc.EndOfLine ) { |
| 76 | + if ( tc.Char() == '*' && tc.Char() == '/' ) { |
| 77 | + tc.Skip(2); |
| 78 | + status = stText; |
| 79 | + break; |
| 80 | + } else { |
| 81 | + tc.Next(); |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + private void ParseString(ITextChars tc) { |
| 87 | + while ( !tc.EndOfLine ) { |
| 88 | + if ( tc.Char() == '\"' ) { |
| 89 | + tc.Next(); |
| 90 | + this.status = stText; |
| 91 | + break; |
| 92 | + } else { |
| 93 | + tc.Next(); |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | +} |
0 commit comments