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