ParserCore.java revision 600c7a4bbc7348167293eac928192e695b4ad5ba
1/*
2* Conditions Of Use
3*
4* This software was developed by employees of the National Institute of
5* Standards and Technology (NIST), an agency of the Federal Government.
6* Pursuant to title 15 Untied States Code Section 105, works of NIST
7* employees are not subject to copyright protection in the United States
8* and are considered to be in the public domain.  As a result, a formal
9* license is not needed to use the software.
10*
11* This software is provided by NIST as a service and is expressly
12* provided "AS IS."  NIST MAKES NO WARRANTY OF ANY KIND, EXPRESS, IMPLIED
13* OR STATUTORY, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF
14* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT
15* AND DATA ACCURACY.  NIST does not warrant or make any representations
16* regarding the use of the software or the results thereof, including but
17* not limited to the correctness, accuracy, reliability or usefulness of
18* the software.
19*
20* Permission to use this software is contingent upon your acceptance
21* of the terms of this agreement
22*
23* .
24*
25*/
26package gov.nist.core;
27
28import java.text.ParseException;
29
30/** Generic parser class.
31* All parsers inherit this class.
32*
33*@version 1.2
34*
35*@author M. Ranganathan   <br/>
36*
37*
38*
39*/
40public abstract class ParserCore {
41    public static final boolean debug = Debug.parserDebug;
42
43    static int nesting_level;
44
45    protected LexerCore lexer;
46
47
48    protected NameValue nameValue(char separator) throws ParseException  {
49        if (debug) dbg_enter("nameValue");
50        try {
51
52        lexer.match(LexerCore.ID);
53        Token name = lexer.getNextToken();
54        // eat white space.
55        lexer.SPorHT();
56        try {
57
58
59                boolean quoted = false;
60
61            char la = lexer.lookAhead(0);
62
63            if (la == separator ) {
64                lexer.consume(1);
65                lexer.SPorHT();
66                String str = null;
67                boolean isFlag = false;
68                if (lexer.lookAhead(0) == '\"')  {
69                     str = lexer.quotedString();
70                     quoted = true;
71                } else {
72                   lexer.match(LexerCore.ID);
73                   Token value = lexer.getNextToken();
74                   str = value.tokenValue;
75
76                   // JvB: flag parameters must be empty string!
77                   if (str==null) {
78                       str = "";
79                       isFlag = true;
80                   }
81                }
82                NameValue nv = new NameValue(name.tokenValue,str,isFlag);
83                if (quoted) nv.setQuotedValue();
84                return nv;
85            }  else {
86                // JvB: flag parameters must be empty string!
87                return new NameValue(name.tokenValue,"",true);
88            }
89        } catch (ParseException ex) {
90            return new NameValue(name.tokenValue,null,false);
91        }
92
93        } finally {
94            if (debug) dbg_leave("nameValue");
95        }
96
97
98    }
99
100    protected  void dbg_enter(String rule) {
101        StringBuffer stringBuffer = new StringBuffer();
102        for (int i = 0; i < nesting_level ; i++)
103            stringBuffer.append(">");
104
105        if (debug)  {
106            System.out.println(
107                stringBuffer + rule +
108                "\nlexer buffer = \n" +
109                lexer.getRest());
110        }
111        nesting_level++;
112    }
113
114    protected void dbg_leave(String rule) {
115        StringBuffer stringBuffer = new StringBuffer();
116        for (int i = 0; i < nesting_level ; i++)
117            stringBuffer.append("<");
118
119        if (debug)  {
120            System.out.println(
121                stringBuffer +
122                rule +
123                "\nlexer buffer = \n" +
124                lexer.getRest());
125        }
126        nesting_level --;
127    }
128
129    protected NameValue nameValue() throws ParseException  {
130        return nameValue('=');
131    }
132
133
134
135    protected void peekLine(String rule) {
136        if (debug) {
137            Debug.println(rule +" " + lexer.peekLine());
138        }
139    }
140}
141
142
143