~ubuntu-branches/ubuntu/saucy/ricochet/saucy

« back to all changes in this revision

Viewing changes to ricochet_0.1.orig/rr-lex.5c

  • Committer: Package Import Robot
  • Author(s): Keith Packard
  • Date: 2012-06-11 13:37:57 UTC
  • Revision ID: package-import@ubuntu.com-20120611133757-zn0ukd22vz56ymto
Tags: 0.3
* Improve appearance of board
* Fix user list when removing/adding same user

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * $Id$
3
 
 *
4
 
 * Copyright © 2003 Keith Packard
5
 
 *
6
 
 * Permission to use, copy, modify, distribute, and sell this software and its
7
 
 * documentation for any purpose is hereby granted without fee, provided that
8
 
 * the above copyright notice appear in all copies and that both that
9
 
 * copyright notice and this permission notice appear in supporting
10
 
 * documentation, and that the name of Keith Packard not be used in
11
 
 * advertising or publicity pertaining to distribution of the software without
12
 
 * specific, written prior permission.  Keith Packard makes no
13
 
 * representations about the suitability of this software for any purpose.  It
14
 
 * is provided "as is" without express or implied warranty.
15
 
 *
16
 
 * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17
 
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18
 
 * EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
19
 
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
20
 
 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
21
 
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
22
 
 * PERFORMANCE OF THIS SOFTWARE.
23
 
 */
24
 
 
25
 
autoload RR
26
 
autoload Ctype
27
 
 
28
 
extend namespace RR {
29
 
    public namespace Lex {
30
 
 
31
 
        public exception syntax ();
32
 
        
33
 
        int lexc (file f) {
34
 
            try {
35
 
                return File::getc (f);
36
 
            } catch File::io_error (string reason, File::error_type error, file f) {
37
 
                switch (error) {
38
 
                case File::error_type.UTF8:
39
 
                    printf ("invalid utf\n");
40
 
                    break;
41
 
                default:
42
 
                    raise File::io_error (reason, error, f);
43
 
                }
44
 
            }
45
 
            return lexc (f);
46
 
        }
47
 
 
48
 
        bool skipwhite (file f, bool allow_newline) {
49
 
            while ((int c = lexc (f)) != -1) {
50
 
                if (c == '\n' && !allow_newline) {
51
 
                    File::ungetc (c, f);
52
 
                    return true;
53
 
                }
54
 
                if (!Ctype::isspace(c)) {
55
 
                    File::ungetc (c, f);
56
 
                    return false;
57
 
                }
58
 
            }
59
 
            return true;
60
 
        }
61
 
 
62
 
        public void skipline (file f) {
63
 
            while ((int c = lexc (f)) != -1)
64
 
            {
65
 
                if (c == '\n')
66
 
                    break;
67
 
            }
68
 
        }
69
 
 
70
 
        string next_word (file f) {
71
 
            string  s = "";
72
 
            bool quoted = false;
73
 
            bool escaped = false;
74
 
            while ((int c = lexc (f)) != -1) {
75
 
                if (!escaped)
76
 
                {
77
 
                    if (c == '\\')
78
 
                    {
79
 
                        escaped = true;
80
 
                        continue;
81
 
                    }
82
 
                    if (c == '"')
83
 
                    {
84
 
                        quoted = !quoted;
85
 
                        continue;
86
 
                    }
87
 
                }
88
 
                if (!quoted && !escaped)
89
 
                {
90
 
                    if (Ctype::isspace (c))
91
 
                    {
92
 
                        File::ungetc (c, f);
93
 
                        break;
94
 
                    }
95
 
                }
96
 
                s = s + String::new(c);
97
 
                escaped = false;
98
 
            }
99
 
            return s;
100
 
        }
101
 
 
102
 
        public bool eol (file f) {
103
 
            skipwhite (f, false);
104
 
            int c = lexc (f);
105
 
            if (c == -1)
106
 
                return true;
107
 
            File::ungetc (c, f);
108
 
            if (c == '\n')
109
 
                return true;
110
 
            return false;
111
 
        }
112
 
 
113
 
        public string word (file f) {
114
 
            if (skipwhite (f, false)) {
115
 
                skipline(f);
116
 
                raise syntax ();
117
 
            }
118
 
            return next_word (f);
119
 
        }
120
 
 
121
 
        public string firstword (file f) {
122
 
            skipwhite (f, true);
123
 
            return next_word (f);
124
 
        }
125
 
 
126
 
        public string[] recv(file f) {
127
 
 
128
 
            string[...] words = {};
129
 
 
130
 
            try {
131
 
                void add (string w) {
132
 
                    words[dim(words)] = w;
133
 
                }
134
 
 
135
 
                string w = firstword (f);
136
 
                add (w);
137
 
                while (!eol (f))
138
 
                    add (word(f));
139
 
                skipline(f);
140
 
            }
141
 
            return words;
142
 
        }
143
 
    }
144
 
}