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