|
| 1 | +/* |
| 2 | + * [The "BSD license"] |
| 3 | + * Copyright (c) 2014 Terence Parr |
| 4 | + * Copyright (c) 2014 Sam Harwell |
| 5 | + * Copyright (c) 2017 Chan Chung Kwong |
| 6 | + * All rights reserved. |
| 7 | + * |
| 8 | + * Redistribution and use in source and binary forms, with or without |
| 9 | + * modification, are permitted provided that the following conditions |
| 10 | + * are met: |
| 11 | + * |
| 12 | + * 1. Redistributions of source code must retain the above copyright |
| 13 | + * notice, this list of conditions and the following disclaimer. |
| 14 | + * 2. Redistributions in binary form must reproduce the above copyright |
| 15 | + * notice, this list of conditions and the following disclaimer in the |
| 16 | + * documentation and/or other materials provided with the distribution. |
| 17 | + * 3. The name of the author may not be used to endorse or promote products |
| 18 | + * derived from this software without specific prior written permission. |
| 19 | + * |
| 20 | + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 21 | + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 22 | + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 23 | + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 24 | + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 25 | + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 | + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 | + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 29 | + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | + */ |
| 31 | +import 'package:antlr4/antlr4.dart'; |
| 32 | +import 'dart:io'; |
| 33 | +import 'dart:convert'; |
| 34 | + |
| 35 | +abstract class JavaLexerBase extends Lexer |
| 36 | +{ |
| 37 | + JavaLexerBase(CharStream input) : super(input) |
| 38 | + { |
| 39 | + } |
| 40 | + |
| 41 | + bool Check1() |
| 42 | + { |
| 43 | + return Character.isJavaIdentifierStart(inputStream.LA(-1)!); |
| 44 | + } |
| 45 | + |
| 46 | + bool Check2() |
| 47 | + { |
| 48 | + return Character.isJavaIdentifierStart(Character.toCodePoint(inputStream.LA(-2)!, inputStream.LA(-1)!)); |
| 49 | + } |
| 50 | + |
| 51 | + bool Check3() |
| 52 | + { |
| 53 | + return Character.isJavaIdentifierPart(inputStream.LA(-1)!); |
| 54 | + } |
| 55 | + |
| 56 | + bool Check4() |
| 57 | + { |
| 58 | + return Character.isJavaIdentifierPart(Character.toCodePoint(inputStream.LA(-2)!, inputStream.LA(-1)!)); |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +class Character |
| 63 | +{ |
| 64 | + static bool isJavaIdentifierPart(int c) |
| 65 | + { |
| 66 | + if (('a'.codeUnitAt(0) <= c && c <= 'z'.codeUnitAt(0)) || ('A'.codeUnitAt(0) <= c && c <= 'Z'.codeUnitAt(0))) //Char.IsLetter((char)c)) |
| 67 | + return true; |
| 68 | + else if (c == '\$'.codeUnitAt(0)) |
| 69 | + return true; |
| 70 | + else if (c == '_'.codeUnitAt(0)) |
| 71 | + return true; |
| 72 | + else if ('0'.codeUnitAt(0) <= c && c <= '9'.codeUnitAt(0)) //Char.IsDigit(c)) |
| 73 | + return true; |
| 74 | + else if ('0'.codeUnitAt(0) <= c && c <= '9'.codeUnitAt(0)) //Char.IsNumber(c)) |
| 75 | + return true; |
| 76 | + return false; |
| 77 | + } |
| 78 | + |
| 79 | + static bool isJavaIdentifierStart(int c) |
| 80 | + { |
| 81 | + if (('a'.codeUnitAt(0) <= c && c <= 'z'.codeUnitAt(0)) || ('A'.codeUnitAt(0) <= c && c <= 'Z'.codeUnitAt(0))) //Char.IsLetter((char)c)) |
| 82 | + return true; |
| 83 | + else if (c == '\$'.codeUnitAt(0)) |
| 84 | + return true; |
| 85 | + else if (c == '_'.codeUnitAt(0)) |
| 86 | + return true; |
| 87 | + return false; |
| 88 | + } |
| 89 | + |
| 90 | + static int toCodePoint(int high, int low) |
| 91 | + { |
| 92 | + List<int> encoded = List.filled(2, 0, growable: false); |
| 93 | + encoded.add(high); |
| 94 | + encoded.add(low); |
| 95 | + return base64.encode(encoded).codeUnitAt(0); |
| 96 | + |
| 97 | +// return Utf16CodeUnitDecoder(encoded).codeUnitAt(0); |
| 98 | +// return utf16.decode(encoded); |
| 99 | +// return Char.ConvertToUtf32(high, low); |
| 100 | + } |
| 101 | +} |
| 102 | + |
0 commit comments