View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.jexl3.parser;
18  
19  /**
20   * Identifiers, variables, ie symbols.
21   */
22  public class ASTIdentifier extends JexlNode {
23      /**
24       *
25       */
26      private static final long serialVersionUID = 1L;
27      protected String name = null;
28      protected int symbol = -1;
29      protected int flags = 0;
30  
31      /** The redefined variable flag. */
32      private static final int REDEFINED = 0;
33      /** The shaded variable flag. */
34      private static final int SHADED = 1;
35      /** The captured variable flag. */
36      private static final int CAPTURED = 2;
37      /** The lexical variable flag. */
38      private static final int LEXICAL = 3;
39      /** The const variable flag. */
40      private static final int CONST = 4;
41  
42      ASTIdentifier(final int id) {
43          super(id);
44      }
45  
46      ASTIdentifier(final Parser p, final int id) {
47          super(p, id);
48      }
49  
50      @Override
51      public String toString() {
52          return name;
53      }
54  
55      void setSymbol(final String identifier) {
56          if (identifier.charAt(0) == '#') {
57              symbol = Integer.parseInt(identifier.substring(1));
58          }
59          name = identifier;
60      }
61  
62      void setSymbol(final int r, final String identifier) {
63          symbol = r;
64          name = identifier;
65      }
66  
67      public int getSymbol() {
68          return symbol;
69      }
70  
71      /**
72       * Sets the value of a flag in a mask.
73       * @param ordinal the flag ordinal
74       * @param mask the flags mask
75       * @param value true or false
76       * @return the new flags mask value
77       */
78      private static int set(final int ordinal, final int mask, final boolean value) {
79          return value? mask | (1 << ordinal) : mask & ~(1 << ordinal);
80      }
81  
82      /**
83       * Checks the value of a flag in the mask.
84       * @param ordinal the flag ordinal
85       * @param mask the flags mask
86       * @return the mask value with this flag or-ed in
87       */
88      private static boolean isSet(final int ordinal, final int mask) {
89          return (mask & 1 << ordinal) != 0;
90      }
91  
92      public void setRedefined(final boolean f) {
93          flags = set(REDEFINED, flags, f);
94      }
95  
96      public boolean isRedefined() {
97          return isSet(REDEFINED, flags);
98      }
99  
100     public void setShaded(final boolean f) {
101         flags = set(SHADED, flags, f);
102     }
103 
104     public boolean isShaded() {
105         return isSet(SHADED, flags);
106     }
107 
108     public void setCaptured(final boolean f) {
109         flags = set(CAPTURED, flags, f);
110     }
111 
112     public boolean isCaptured() {
113         return isSet(CAPTURED, flags);
114     }
115 
116     public boolean isLexical() {
117         return isSet(LEXICAL, flags);
118     }
119 
120     public void setLexical(final boolean f) {
121         flags = set(LEXICAL, flags, f);
122     }
123 
124     @Override
125     public boolean isConstant() {
126         return isSet(CONST, flags);
127     }
128 
129     public void setConstant(final boolean f) {
130         flags = set(CONST, flags, f);
131     }
132 
133     public String getName() {
134         return name;
135     }
136 
137     public String getNamespace() {
138         return null;
139     }
140 
141     @Override
142     public Object jjtAccept(final ParserVisitor visitor, final Object data) {
143         return visitor.visit(this, data);
144     }
145 }