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    *      https://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  
28      /** The redefined variable flag. */
29      private static final int REDEFINED = 0;
30  
31      /** The shaded variable flag. */
32      private static final int SHADED = 1;
33  
34      /** The captured variable flag. */
35      private static final int CAPTURED = 2;
36  
37      /** The lexical variable flag. */
38      private static final int LEXICAL = 3;
39  
40      /** The const variable flag. */
41      private static final int CONST = 4;
42  
43      /**
44       * Checks the value of a flag in the mask.
45       *
46       * @param ordinal the flag ordinal
47       * @param mask the flags mask
48       * @return the mask value with this flag or-ed in
49       */
50      private static boolean isSet(final int ordinal, final int mask) {
51          return (mask & 1 << ordinal) != 0;
52      }
53  
54      /**
55       * Sets the value of a flag in a mask.
56       *
57       * @param ordinal the flag ordinal
58       * @param mask the flags mask
59       * @param value true or false
60       * @return the new flags mask value
61       */
62      private static int set(final int ordinal, final int mask, final boolean value) {
63          return value? mask | 1 << ordinal : mask & ~(1 << ordinal);
64      }
65      protected String name;
66  
67      protected int symbol = -1;
68  
69      protected int flags;
70  
71      ASTIdentifier(final int id) {
72          super(id);
73      }
74  
75      public String getName() {
76          return name;
77      }
78  
79      public String getNamespace() {
80          return null;
81      }
82  
83      public int getSymbol() {
84          return symbol;
85      }
86  
87      public boolean isCaptured() {
88          return isSet(CAPTURED, flags);
89      }
90  
91      @Override
92      public boolean isConstant() {
93          return isSet(CONST, flags);
94      }
95  
96      public boolean isLexical() {
97          return isSet(LEXICAL, flags);
98      }
99  
100     public boolean isRedefined() {
101         return isSet(REDEFINED, flags);
102     }
103 
104     public boolean isShaded() {
105         return isSet(SHADED, flags);
106     }
107 
108     @Override
109     public Object jjtAccept(final ParserVisitor visitor, final Object data) {
110         return visitor.visit(this, data);
111     }
112 
113     public void setCaptured(final boolean f) {
114         flags = set(CAPTURED, flags, f);
115     }
116 
117     public void setConstant(final boolean f) {
118         flags = set(CONST, flags, f);
119     }
120 
121     public void setLexical(final boolean f) {
122         flags = set(LEXICAL, flags, f);
123     }
124 
125     public void setRedefined(final boolean f) {
126         flags = set(REDEFINED, flags, f);
127     }
128 
129     public void setShaded(final boolean f) {
130         flags = set(SHADED, flags, f);
131     }
132 
133     void setSymbol(final int r, final String identifier) {
134         symbol = r;
135         name = identifier;
136     }
137 
138     void setSymbol(final String identifier) {
139         if (identifier.charAt(0) == '#') {
140             symbol = Integer.parseInt(identifier.substring(1));
141         }
142         name = identifier;
143     }
144 
145     @Override
146     public String toString() {
147         return name;
148     }
149 }