]> git.lyx.org Git - lyx.git/blob - lib/lyx2lyx/parser_tools.py
e459cedd48f801d91b73f0bf853c17ec69ff346c
[lyx.git] / lib / lyx2lyx / parser_tools.py
1 # This file is part of lyx2lyx
2 # -*- coding: iso-8859-1 -*-
3 # Copyright (C) 2002-2004 Dekel Tsur <dekel@lyx.org>, José Matos <jamatos@lyx.org>
4 #
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 import string
20 import re
21
22 def check_token(line, token):
23     if line[:len(token)] == token:
24         return 1
25     return 0
26
27
28 # We need to check that the char after the token is space, but I think
29 # we can ignore this
30 def find_token(lines, token, start, end = 0):
31     if end == 0:
32         end = len(lines)
33     m = len(token)
34     for i in xrange(start, end):
35         if lines[i][:m] == token:
36             return i
37     return -1
38
39
40 def find_token_exact(lines, token, start, end = 0):
41     if end == 0:
42         end = len(lines)
43     for i in xrange(start, end):
44         x = string.split(lines[i])
45         y = string.split(token)
46         if len(x) < len(y):
47             continue
48         if x[:len(y)] == y:
49             return i
50     return -1
51
52
53 def find_tokens(lines, tokens, start, end = 0):
54     if end == 0:
55         end = len(lines)
56     for i in xrange(start, end):
57         for token in tokens:
58             if lines[i][:len(token)] == token:
59                 return i
60     return -1
61
62
63 def find_tokens_exact(lines, tokens, start, end = 0):
64     if end == 0:
65         end = len(lines)
66     for i in xrange(start, end):
67         for token in tokens:
68             x = string.split(lines[i])
69             y = string.split(token)
70             if len(x) < len(y):
71                 continue
72             if x[:len(y)] == y:
73                 return i
74     return -1
75
76
77 def find_re(lines, rexp, start, end = 0):
78     if end == 0:
79         end = len(lines)
80     for i in xrange(start, end):
81         if rexp.match(lines[i]):
82                 return i
83     return -1
84
85
86 def find_token_backwards(lines, token, start):
87     m = len(token)
88     for i in xrange(start, -1, -1):
89         line = lines[i]
90         if line[:m] == token:
91             return i
92     return -1
93
94
95 def find_tokens_backwards(lines, tokens, start):
96     for i in xrange(start, -1, -1):
97         line = lines[i]
98         for token in tokens:
99             if line[:len(token)] == token:
100                 return i
101     return -1
102
103
104 def get_value(lines, token, start, end = 0):
105     i = find_token_exact(lines, token, start, end)
106     if i == -1:
107         return ""
108     if len(string.split(lines[i])) > 1:
109         return string.split(lines[i])[1]
110     else:
111         return ""
112
113
114 def del_token(lines, token, start, end):
115     k = find_token_exact(lines, token, start, end)
116     if k == -1:
117         return end
118     else:
119         del lines[k]
120         return end - 1
121
122
123 def find_end_of(lines, i, start_token, end_token):
124     count = 1
125     n = len(lines)
126     while i < n:
127         i = find_tokens(lines, [end_token, start_token], i+1)
128         if check_token(lines[i], start_token):
129             count = count+1
130         else:
131             count = count-1
132         if count == 0:
133             return i
134     return -1
135
136
137 # Finds the matching \end_inset
138 def find_beginning_of(lines, i, start_token, end_token):
139     count = 1
140     while i > 0:
141         i = find_tokens_backwards(lines, [start_token, end_token], i-1)
142         if check_token(lines[i], end_token):
143             count = count+1
144         else:
145             count = count-1
146         if count == 0:
147             return i
148     return -1
149
150
151 def is_nonempty_line(line):
152     return line != " "*len(line)
153
154
155 def find_nonempty_line(lines, start, end = 0):
156     if end == 0:
157         end = len(lines)
158     for i in xrange(start, end):
159         if is_nonempty_line(lines[i]):
160             return i
161     return -1