View Javadoc

1   /*
2    * #%L
3    * prolobjectlink-jpi-jtrolog
4    * %%
5    * Copyright (C) 2012 - 2018 WorkLogic Project
6    * %%
7    * This program is free software: you can redistribute it and/or modify
8    * it under the terms of the GNU Lesser General Public License as
9    * published by the Free Software Foundation, either version 2.1 of the
10   * License, or (at your option) any later version.
11   * 
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Lesser Public License for more details.
16   * 
17   * You should have received a copy of the GNU General Lesser Public
18   * License along with this program.  If not, see
19   * <http://www.gnu.org/licenses/lgpl-2.1.html>.
20   * #L%
21   */
22  package jTrolog.parser;
23  
24  import java.io.Serializable;
25  
26  /**
27   * This class represents a token read by the prolog term tokenizer
28   */
29  @SuppressWarnings({ "serial" })
30  class Token implements Serializable {
31  	// token textual representation
32  	String seq;
33  	// token type and attribute
34  	int type;
35  
36  	static final int ATOM = 'A';
37  	static final int SQ_SEQUENCE = 'S';
38  	static final int DQ_SEQUENCE = 'D';
39  	static final int OPERATOR = 'O';
40  	static final int FUNCTOR = 'F';
41  
42  	static final int ATOM_OPERATOR = 'B';
43  	static final int ATOM_FUNCTOR = 'a';
44  	static final int OPERATOR_FUNCTOR = 'p';
45  	static final int SQ_FUNCTOR = 's';
46  
47  	static final int VARIABLE = 'v';
48  	static final int EOF = 'e';
49  	static final int INTEGER = 'i';
50  	static final int FLOAT = 'f';
51  
52  	public Token(String s, int t) {
53  		seq = s.intern();
54  		type = t;
55  	}
56  
57  	public String getValue() {
58  		return seq;
59  	}
60  
61  	public boolean isOperator(boolean commaIsEndMarker) {
62  		if (commaIsEndMarker && ",".equals(seq))
63  			return false;
64  		return type == OPERATOR || type == ATOM_OPERATOR || type == OPERATOR_FUNCTOR;
65  	}
66  
67  	public boolean isFunctor() {
68  		return type == FUNCTOR || type == ATOM_FUNCTOR || type == SQ_FUNCTOR || type == OPERATOR_FUNCTOR;
69  	}
70  
71  	public boolean isNumber() {
72  		return type == INTEGER || type == FLOAT;
73  	}
74  
75  	boolean isEOF() {
76  		return type == EOF;
77  	}
78  
79  	boolean isType(int type) {
80  		return this.type == type;
81  	}
82  
83  	boolean isAtom() {
84  		return type == ATOM || type == ATOM_OPERATOR || type == ATOM_FUNCTOR || type == SQ_FUNCTOR || type == SQ_SEQUENCE || type == DQ_SEQUENCE;
85  	}
86  }