aboutsummaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
authorJussi Pakkanen <jpakkane@gmail.com>2014-05-25 07:57:29 +0300
committerJussi Pakkanen <jpakkane@gmail.com>2014-05-25 07:57:29 +0300
commit7d25129c03905868423381e4df714a88b34e304a (patch)
treecc4d23e627c5d85195af912472803ebafc95427f /tools
parente7ca0ab09b68813fddb97ca4135075df44dd706d (diff)
downloadmeson-7d25129c03905868423381e4df714a88b34e304a.zip
meson-7d25129c03905868423381e4df714a88b34e304a.tar.gz
meson-7d25129c03905868423381e4df714a88b34e304a.tar.bz2
Parse some code.
Diffstat (limited to 'tools')
-rwxr-xr-xtools/cmake2meson.py62
1 files changed, 61 insertions, 1 deletions
diff --git a/tools/cmake2meson.py b/tools/cmake2meson.py
index 71339f2..960cad9 100755
--- a/tools/cmake2meson.py
+++ b/tools/cmake2meson.py
@@ -14,6 +14,66 @@
# See the License for the specific language governing permissions and
# limitations under the License.
+import sys, os
+import re
+
+class Lexer:
+ def __init__(self):
+ self.token_specification = [
+ # Need to be sorted longest to shortest.
+ ('ignore', re.compile(r'[ \t]')),
+ ('id', re.compile('[_0-9a-z/A-Z.]+')),
+ ('eol', re.compile(r'\n')),
+ ('comment', re.compile(r'\#.*')),
+ ('lparen', re.compile(r'\(')),
+ ('rparen', re.compile(r'\)')),
+ ('string', re.compile('"[^"]*?"')),
+ ]
+
+ def lex(self, code):
+ lineno = 1
+ line_start = 0
+ loc = 0;
+ col = 0
+ while(loc < len(code)):
+ matched = False
+ value = None
+ for (tid, reg) in self.token_specification:
+ mo = reg.match(code, loc)
+ if mo:
+ curline = lineno
+ col = mo.start()-line_start
+ matched = True
+ loc = mo.end()
+ match_text = mo.group()
+ if tid == 'ignore' or tid == 'comment':
+ pass
+ elif tid == 'lparen':
+ yield('lparen')
+ elif tid == 'rparen':
+ yield('rparen')
+ elif tid == 'string':
+ yield('String: ' + match_text[1:-1])
+ elif tid == 'id':
+ yield('Id: ' + match_text)
+ elif tid == 'eol':
+ yield('eol')
+ else:
+ raise RuntimeError('Wharrgarbl')
+ break
+ if not matched:
+ raise RuntimeError('Lexer got confused %d %d' % (lineno, col))
+
+def convert(cmake_root):
+ cfile = os.path.join(cmake_root, 'CMakeLists.txt')
+ cmakecode = open(cfile).read()
+ l = Lexer()
+ for t in l.lex(cmakecode):
+ print(t)
+
if __name__ == '__main__':
- pass
+ if len(sys.argv) != 2:
+ print(sys.argv[0], '<CMake project root>')
+ convert(sys.argv[1])
+