Mercurial Hosting > luan
annotate src/luan/modules/parsers/Css.java @ 1562:b89212fd04b5
remove table.luan
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sun, 08 Nov 2020 16:50:59 -0700 |
parents | 27efb1fcbcb5 |
children |
rev | line source |
---|---|
1289 | 1 package luan.modules.parsers; |
2 | |
1333
25746915a241
merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents:
1330
diff
changeset
|
3 import luan.Luan; |
1289 | 4 import luan.LuanTable; |
1330 | 5 import luan.LuanException; |
1402
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1333
diff
changeset
|
6 import goodjava.parser.Parser; |
1289 | 7 |
8 | |
9 public final class Css { | |
10 | |
1562 | 11 public static LuanTable style(String text) { |
1330 | 12 try { |
1562 | 13 return new Css(text).parseStyle(); |
1330 | 14 } catch(LuanException e) { |
15 throw new RuntimeException(e); | |
16 } | |
1289 | 17 } |
18 | |
19 private final Parser parser; | |
20 | |
1562 | 21 private Css(String text) { |
1289 | 22 this.parser = new Parser(text); |
23 } | |
24 | |
1330 | 25 private LuanTable parseStyle() throws LuanException { |
1562 | 26 LuanTable tbl = new LuanTable(); |
1289 | 27 while( matchSpace() ); |
28 while( !parser.endOfInput() ) { | |
29 int start = parser.currentIndex(); | |
30 if( !matchPropertyChar() ) | |
31 return null; | |
32 while( matchPropertyChar() ); | |
33 String prop = parser.textFrom(start).toLowerCase(); | |
34 | |
35 while( matchSpace() ); | |
36 if( !parser.match(':') ) | |
37 return null; | |
38 | |
39 start = parser.currentIndex(); | |
40 while( !parser.endOfInput() && parser.noneOf(";") ); | |
41 String val = parser.textFrom(start).trim(); | |
42 | |
1562 | 43 tbl.rawPut(prop,val); |
1289 | 44 parser.match(';'); |
45 while( matchSpace() ); | |
46 } | |
47 return tbl; | |
48 } | |
49 | |
50 private boolean matchPropertyChar() { | |
51 return parser.inCharRange('a','z') | |
52 || parser.inCharRange('A','Z') | |
53 || parser.inCharRange('0','9') | |
54 || parser.anyOf("_-") | |
55 ; | |
56 } | |
57 | |
58 private boolean matchSpace() { | |
59 return parser.anyOf(" \t\r\n"); | |
60 } | |
61 | |
62 } |