|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
Tokens.java | 50% | 82.4% | 80% | 79.4% |
|
1 | /*BEGIN_COPYRIGHT_BLOCK | |
2 | * | |
3 | * Copyright (c) 2001-2010, JavaPLT group at Rice University (drjava@rice.edu) | |
4 | * All rights reserved. | |
5 | * | |
6 | * Redistribution and use in source and binary forms, with or without | |
7 | * modification, are permitted provided that the following conditions are met: | |
8 | * * Redistributions of source code must retain the above copyright | |
9 | * notice, this list of conditions and the following disclaimer. | |
10 | * * Redistributions in binary form must reproduce the above copyright | |
11 | * notice, this list of conditions and the following disclaimer in the | |
12 | * documentation and/or other materials provided with the distribution. | |
13 | * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the | |
14 | * names of its contributors may be used to endorse or promote products | |
15 | * derived from this software without specific prior written permission. | |
16 | * | |
17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
18 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
19 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
20 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR | |
21 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
22 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
23 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
24 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | |
25 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
26 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
27 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
28 | * | |
29 | * This software is Open Source Initiative approved Open Source Software. | |
30 | * Open Source Initative Approved is a trademark of the Open Source Initiative. | |
31 | * | |
32 | * This file is part of DrJava. Download the current version of this project | |
33 | * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/ | |
34 | * | |
35 | * END_COPYRIGHT_BLOCK*/ | |
36 | ||
37 | package edu.rice.cs.util.sexp; | |
38 | ||
39 | /** A common namespace for the token classes. */ | |
40 | public interface Tokens { | |
41 | ||
42 | /** These tokens are designed to be compared using the == operator with (, ), ", and \. Otherwise, the tokens may be | |
43 | * compared using the .equals() method. This class is concrete only for testing purposes. | |
44 | */ | |
45 | /* abstract */ class SExpToken { | |
46 | protected String _rep; | |
47 | ||
48 | /** @param rep The string representation of this token */ | |
49 | 580 | public SExpToken(String rep) { _rep = rep.intern(); } // intern() supports use of == for equality testing |
50 | ||
51 | /** @return the string representation of this token */ | |
52 | 437 | public String getText() { return _rep; } |
53 | ||
54 | 19 | public boolean equals(Object o) { |
55 | 19 | return (o != null && o.getClass() == getClass() && ((SExpToken)o)._rep == _rep); |
56 | } | |
57 | ||
58 | 0 | public int hashCode() { return _rep.hashCode(); } |
59 | ||
60 | 4 | public String toString() { return _rep; } |
61 | } | |
62 | ||
63 | ////////////// Symbol Tokens /////////////////// | |
64 | ||
65 | class LeftParenToken extends SExpToken { | |
66 | public static final LeftParenToken ONLY = new LeftParenToken(); | |
67 | 4 | private LeftParenToken() { super("("); } |
68 | } | |
69 | ||
70 | class RightParenToken extends SExpToken { | |
71 | public static final RightParenToken ONLY = new RightParenToken(); | |
72 | 4 | private RightParenToken() { super(")"); } |
73 | } | |
74 | ||
75 | class BackSlashToken extends SExpToken { | |
76 | public static final BackSlashToken ONLY = new BackSlashToken(); | |
77 | 1 | private BackSlashToken() { super("\\"); } |
78 | } | |
79 | ||
80 | ////////////// General Tokens ////////////////// | |
81 | ||
82 | /** Words include any text (including symbols) that is not a number, a backslash, or a quote character. */ | |
83 | 317 | class WordToken extends SExpToken { public WordToken(String word) { super(word); } } |
84 | ||
85 | /** This token is handled as a unit by the lexer. Any text between the pair of quotes is given | |
86 | * in this QuotedTextToken. | |
87 | */ | |
88 | class QuotedTextToken extends SExpToken { | |
89 | // private String _txt; | |
90 | 123 | public QuotedTextToken(String txt) { super(txt); } |
91 | 0 | public String getFullText() { return "\"" + _rep + "\""; } |
92 | } | |
93 | ||
94 | /** Words include any text (including symbols) that is not a number, a backslash, or a quote character. */ | |
95 | class BooleanToken extends SExpToken { | |
96 | public static final BooleanToken TRUE = new BooleanToken(true); | |
97 | public static final BooleanToken FALSE = new BooleanToken(false); | |
98 | ||
99 | private boolean _bool; | |
100 | 8 | private BooleanToken(boolean bool){ |
101 | 8 | super("" + bool); |
102 | 8 | _bool = bool; |
103 | } | |
104 | 0 | public boolean getValue() { return _bool; } |
105 | } | |
106 | ||
107 | /** Numbers are string s of only digits (0-9) | |
108 | */ | |
109 | class NumberToken extends SExpToken { | |
110 | private double _num; | |
111 | 117 | public NumberToken(double num){ |
112 | // If it is a whole number, don't include | |
113 | // the decimal in the string representation | |
114 | 117 | super((num % 1 == 0) ? "" + (int)num : "" + num); |
115 | 117 | _num = num; |
116 | } | |
117 | 114 | public double getValue() { return _num; } |
118 | } | |
119 | ||
120 | } |
|