001: /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
002: * This file is part of Beaver Parser Generator. *
003: * Copyright (C) 2003,2004 Alexander Demenchuk <alder@softanvil.com>. *
004: * All rights reserved. *
005: * See the file "LICENSE" for the terms and conditions for copying, *
006: * distribution and modification of Beaver. *
007: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
008:
009: package beaver;
010:
011: /**
012: * Represents a symbol of a grammar.
013: */
014: public class Symbol {
015: static private final int COLUMN_FIELD_BITS = 12;
016: static private final int COLUMN_FIELD_MASK = (1 << COLUMN_FIELD_BITS) - 1;
017:
018: /**
019: * Packes symbol "coordinates" into a single number.
020: */
021: static public int makePosition(int line, int column) {
022: return line << COLUMN_FIELD_BITS | column;
023: }
024:
025: /**
026: * Extracts line number from a packed position.
027: */
028: static public int getLine(int position) {
029: return position >>> COLUMN_FIELD_BITS;
030: }
031:
032: /**
033: * Extracts column number from a packed position.
034: */
035: static public int getColumn(int position) {
036: return position & COLUMN_FIELD_MASK;
037: }
038:
039: /**
040: * Value assigned to this symbol.
041: */
042: public final Object value;
043:
044: /**
045: * Numeric symbol ID.
046: */
047: protected short id;
048:
049: /**
050: * Line and column where this symbol begins.
051: */
052: protected int start;
053:
054: /**
055: * Line and column where this symbol ends.
056: */
057: protected int end;
058:
059: public Symbol(short id) {
060: this .id = id;
061: this .value = null;
062: }
063:
064: public Symbol(short id, Object value) {
065: this .id = id;
066: this .value = value;
067: }
068:
069: public Symbol(short id, int start, int end) {
070: this .id = id;
071: this .value = null;
072: this .start = start;
073: this .end = end;
074: }
075:
076: public Symbol(short id, int left, int right, Object value) {
077: this .id = id;
078: this .value = value;
079: this .start = left;
080: this .end = right;
081: }
082:
083: public Symbol(short id, int start_line, int start_column, int length) {
084: this .id = id;
085: this .value = null;
086: this .start = makePosition(start_line, start_column);
087: this .end = makePosition(start_line, start_column + length - 1);
088: }
089:
090: public Symbol(short id, int start_line, int start_column,
091: int length, Object value) {
092: this .id = id;
093: this .value = value;
094: this .start = makePosition(start_line, start_column);
095: this .end = makePosition(start_line, start_column + length - 1);
096: }
097:
098: /**
099: * Creates Symbol for non-symbolic results of action routines
100: *
101: * @param value attached Symbol's value
102: */
103: public Symbol(Object value) {
104: this .value = value;
105: }
106:
107: /**
108: * Special case constructor that allows creation of explicitly Symbol-ized nonterminals.
109: * <p/>
110: * Used by classes descending from Symbol and which instances are returned by reduce actions.
111: * In this case ID and symbol position will be assigned by the parser when reduce action
112: * code returns this symbol.
113: */
114: protected Symbol() {
115: this .value = this ;
116: }
117:
118: /**
119: * Returns an ID of this symbol.
120: * <p/>
121: * This ID typically is, depending on a symbol type, either a terminal ID if a Symbol is a token
122: * created and returned by a Scanner, or a nonterminal ID if a symbol was created by parser based
123: * on that nonterminal definition. In the former case the ID is one of IDs generated by Beaver
124: * for terminal symbols. The latter keeps IDs of nonterminal symbols.
125: *
126: * @return symbol's ID
127: */
128: public short getId() {
129: return id;
130: }
131:
132: /**
133: * Returns a position in a source where this symbol starts.
134: *
135: * @return packed line and column numbers
136: */
137: public int getStart() {
138: return start;
139: }
140:
141: /**
142: * Returns a position in a source where this symbol ends.
143: *
144: * @return packed line and column numbers
145: */
146: public int getEnd() {
147: return end;
148: }
149: }
|