1 /* 2 * The string related lexical rules are derived from rules from the 3 * Java 1.6 grammar which can be found here: 4 * http://openjdk.java.net/projects/compiler-grammar/antlrworks/Java.g 5 * 6 * Specifically, these rules: 7 * 8 * HEX_PREFIX, HEX_DIGIT, ESCAPE_SEQUENCE, STRING_LITERAL, BASE_STRING_LITERAL 9 * 10 * These rules were originally copyrighted by Terence Parr, and are used here in 11 * accordance with the following license 12 * 13 * [The "BSD licence"] 14 * Copyright (c) 2007-2008 Terence Parr 15 * All rights reserved. 16 * 17 * Redistribution and use in source and binary forms, with or without 18 * modification, are permitted provided that the following conditions 19 * are met: 20 * 1. Redistributions of source code must retain the above copyright 21 * notice, this list of conditions and the following disclaimer. 22 * 2. Redistributions in binary form must reproduce the above copyright 23 * notice, this list of conditions and the following disclaimer in the 24 * documentation and/or other materials provided with the distribution. 25 * 3. The name of the author may not be used to endorse or promote products 26 * derived from this software without specific prior written permission. 27 * 28 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 29 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 30 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 31 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 33 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 34 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 35 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 36 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 37 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 38 * 39 * 40 * The remainder of this grammar is released by me (Ben Gruver) under the 41 * following license: 42 * 43 * [The "BSD licence"] 44 * Copyright (c) 2010 Ben Gruver 45 * All rights reserved. 46 * 47 * Redistribution and use in source and binary forms, with or without 48 * modification, are permitted provided that the following conditions 49 * are met: 50 * 1. Redistributions of source code must retain the above copyright 51 * notice, this list of conditions and the following disclaimer. 52 * 2. Redistributions in binary form must reproduce the above copyright 53 * notice, this list of conditions and the following disclaimer in the 54 * documentation and/or other materials provided with the distribution. 55 * 3. The name of the author may not be used to endorse or promote products 56 * derived from this software without specific prior written permission. 57 * 58 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 59 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 60 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 61 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 62 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 63 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 64 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 65 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 66 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 67 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 68 */ 69 70 grammar expectedTokensTestGrammar; 71 72 @lexer::header { 73 package org.jf.smali; 74 } 75 76 @parser::header { 77 package org.jf.smali; 78 79 import java.util.Collections; 80 } 81 82 @parser::members { 83 public static class ExpectedToken { 84 public final String tokenName; 85 public final String tokenText; 86 87 public ExpectedToken(String tokenName, String tokenText) { 88 this.tokenName = tokenName; 89 this.tokenText = tokenText; 90 } 91 92 public ExpectedToken(String tokenName) { 93 this.tokenName = tokenName; 94 this.tokenText = null; 95 } 96 } 97 98 private final ArrayList<ExpectedToken> expectedTokens = new ArrayList<ExpectedToken>(); 99 100 public List<ExpectedToken> getExpectedTokens() { 101 return Collections.unmodifiableList(expectedTokens); 102 } 103 } 104 105 106 fragment HEX_DIGIT 107 : ('0'..'9')|('A'..'F')|('a'..'f'); 108 109 fragment HEX_DIGITS 110 : HEX_DIGIT HEX_DIGIT HEX_DIGIT HEX_DIGIT; 111 112 fragment ESCAPE_SEQUENCE[StringBuilder sb] 113 : '\\' 114 ( 115 'b' {sb.append("\b");} 116 | 't' {sb.append("\t");} 117 | 'n' {sb.append("\n");} 118 | 'f' {sb.append("\f");} 119 | 'r' {sb.append("\r");} 120 | '\"' {sb.append("\"");} 121 | '\'' {sb.append("'");} 122 | '\\' {sb.append("\\");} 123 | 'u' HEX_DIGITS {sb.append((char)Integer.parseInt($HEX_DIGITS.text, 16));} 124 ); 125 126 127 STRING_LITERAL 128 @init {StringBuilder sb = new StringBuilder();} 129 : BASE_STRING_LITERAL[sb] {setText(sb.toString());}; 130 131 fragment BASE_STRING_LITERAL[StringBuilder sb] 132 : '"' 133 ( ESCAPE_SEQUENCE[sb] 134 | ~( '\\' | '"' | '\r' | '\n' ) {sb.append((char)input.LA(-1));} 135 )* 136 '"'; 137 138 TOKEN_NAME 139 : (('a'..'z')|('A' .. 'Z')|'_'|('0'..'9'))+; 140 141 WHITE_SPACE 142 : (' '|'\t'|'\n'|'\r')+ {$channel = HIDDEN;}; 143 144 top : token*; 145 146 token : TOKEN_NAME ( '(' STRING_LITERAL ')' ) 147 { 148 expectedTokens.add(new ExpectedToken($TOKEN_NAME.getText(), $STRING_LITERAL.getText())); 149 } | 150 TOKEN_NAME 151 { 152 expectedTokens.add(new ExpectedToken($TOKEN_NAME.getText())); 153 };