keep alpha source states during node propagation
[IRC.git] / Robust / src / Benchmarks / mlp / tagger / mlp-smaller / PropertyParser.java
1 /**\r
2  * PropertyParser class\r
3  * Parses property files\r
4  * <p>\r
5  * <code>int</code>.\r
6  *\r
7  * @author  Daniel Jackson\r
8  * @version 0, 07/02/01\r
9  */\r
10 \r
11 //package tagger;\r
12 //import java.io.*;\r
13 //import java.util.*;\r
14 \r
15 public class PropertyParser {\r
16   private FileInputStream reader;\r
17   private String token;\r
18   private int next_char;\r
19 \r
20   public PropertyParser (FileInputStream r) {\r
21     reader = r;\r
22     next_char = reader.read ();\r
23     consume_comments ();\r
24   }\r
25   \r
26   private void consume_comments () {\r
27     // consume lines that don't start with <\r
28     while (next_char != '<' && !is_eos (next_char)) {\r
29       if (!is_eol (next_char))\r
30         reader.readLine ();\r
31       consume_char ();\r
32     }\r
33   }\r
34   \r
35   private void consume_char () {\r
36     token += (char) next_char;\r
37     next_char = reader.read ();\r
38     //while(next_char == 13 || next_char==10) {\r
39     //  next_char = reader.read ();\r
40     //}\r
41     //System.out.println( "next_char: "+(char)next_char );\r
42   }\r
43   \r
44   private void error (String msg) {\r
45     // correct to number from 1, not zero\r
46     //t line_number = reader.getLineNumber() + 1;\r
47     System.out.println (msg);\r
48     System.exit(-1);\r
49   }\r
50   \r
51   public boolean has_more_properties () {\r
52     return (!is_eos (next_char));\r
53   }\r
54   \r
55   /**\r
56    * requires: next_char contains next character in reader <p>\r
57    * ensures: returns list of properties until end of line or stream <p>\r
58    *    according to the following syntax:\r
59    *            property list is sequence of properties followed by eol of eos\r
60    *            property is left-angle, property-name, colon, value, right-angle\r
61    *            property-name is alphanumeric string, but value is any char sequence\r
62    *    skips lines that do not start with <\r
63    *    reports syntax errors on this.error_reporter\r
64    *    Syntax\r
65    * @return list of properties until end of line or stream.\r
66    *    Notes: chose LinkedList because it provides removeFirst, to support common\r
67    *    case in which first property is removed (eg, because it's a style name)\r
68    */\r
69   public LinkedList get_property_list () {\r
70     LinkedList result = /*disjoint llPropList*/ new LinkedList ();\r
71     while (!is_eol (next_char) && !is_eos(next_char))\r
72       result.add (get_property ());\r
73     consume_char ();\r
74     consume_comments ();\r
75     return result;\r
76   }\r
77   \r
78   private Property get_property () {    \r
79     if (next_char != '<')\r
80       error ("Found " + next_char + " when expecting <");\r
81     consume_char ();\r
82     token = "";\r
83     while (is_alphanumeric (next_char)) consume_char ();\r
84     String property = token;\r
85     if (next_char != ':')\r
86       error ("Found " + next_char + " following " + token + " when expecting :");\r
87     consume_char ();\r
88     token = "";\r
89     while (next_char != '>' && !is_eol(next_char) && !is_eos (next_char))\r
90       consume_char ();\r
91     String value = token;\r
92     if (next_char != '>')\r
93       error ("Found " + next_char + " following " + token + " when expecting >");\r
94     consume_char ();\r
95     return new Property (property, value);\r
96   }\r
97   \r
98   static boolean is_eol (int c) {return c == '\n';}\r
99   static boolean is_eos (int c) {return c == -1;}\r
100   static boolean is_alphabetic (int c) {\r
101     return c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z';\r
102   }\r
103   static boolean is_numeric (int c) {return c >= '0' && c <= '9';}\r
104   static boolean is_alphanumeric (int c) {\r
105     return is_numeric (c) || is_alphabetic (c);\r
106   }\r
107 }\r