First NIT release and new clean mercurial repository
[nit.git] / src / utils.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2006-2008 Floréal Morandat <morandat@lirmm.fr>
4 # Copyright 2008 Jean Privat <jean@pryen.org>
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17
18 # Various functions for NIT tools
19 package utils
20
21 # Mangle an array of symbol using only alphanums and underscores
22 meth cmangle(symbols: Symbol...): String
23 do
24 var table = once cmangle_table
25 var res = new String
26 for sym in symbols do
27 if not res.is_empty then
28 res.add('_')
29 res.add('_')
30 res.add('_')
31 end
32 var underscore = false
33 var normal = true
34 var s = sym.to_s
35 for c in s do
36 if (c >= 'a' and c <= 'z') or (c >='A' and c <= 'Z') or (c >= '0' and c <= '9') then
37 res.add(c)
38 underscore = false
39 normal = true
40 else if c == '_' and not underscore then
41 res.add(c)
42 underscore = true
43 normal = true
44 else if table.has_key(c) then
45 if normal then
46 res.add('_')
47 res.add('_')
48 end
49 res.append(table[c])
50 normal = false
51 underscore = false
52 end
53 end
54 end
55 return res
56 end
57
58 # Build the table that associates character to mangle to string
59 private meth cmangle_table: HashMap[Char, String]
60 do
61 var res = new HashMap[Char, String]
62 res['+'] = "plus"
63 res['-'] = "minus"
64 res['*'] = "star"
65 res['/'] = "slash"
66 res['%'] = "percent"
67 res['['] = "bra"
68 res['='] = "eq"
69 res['<'] = "l"
70 res['>'] = "g"
71 res['!'] = "n"
72 res['_'] = "u"
73 res['@'] = "at"
74 return res
75 end
76