Mercurial Hosting > luan
annotate src/luan/modules/parsers/Css.java @ 1518:f989778ca9e1
stringify, json_string
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Fri, 19 Jun 2020 17:50:25 -0600 |
parents | 27efb1fcbcb5 |
children | b89212fd04b5 |
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 | |
1333
25746915a241
merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents:
1330
diff
changeset
|
11 public static LuanTable style(Luan luan,String text) { |
1330 | 12 try { |
13 return new Css(luan,text).parseStyle(); | |
14 } catch(LuanException e) { | |
15 throw new RuntimeException(e); | |
16 } | |
1289 | 17 } |
18 | |
1333
25746915a241
merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents:
1330
diff
changeset
|
19 private final Luan luan; |
1289 | 20 private final Parser parser; |
21 | |
1333
25746915a241
merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents:
1330
diff
changeset
|
22 private Css(Luan luan,String text) { |
1289 | 23 this.luan = luan; |
24 this.parser = new Parser(text); | |
25 } | |
26 | |
1330 | 27 private LuanTable parseStyle() throws LuanException { |
1289 | 28 LuanTable tbl = new LuanTable(luan); |
29 while( matchSpace() ); | |
30 while( !parser.endOfInput() ) { | |
31 int start = parser.currentIndex(); | |
32 if( !matchPropertyChar() ) | |
33 return null; | |
34 while( matchPropertyChar() ); | |
35 String prop = parser.textFrom(start).toLowerCase(); | |
36 | |
37 while( matchSpace() ); | |
38 if( !parser.match(':') ) | |
39 return null; | |
40 | |
41 start = parser.currentIndex(); | |
42 while( !parser.endOfInput() && parser.noneOf(";") ); | |
43 String val = parser.textFrom(start).trim(); | |
44 | |
1330 | 45 tbl.put(prop,val); |
1289 | 46 parser.match(';'); |
47 while( matchSpace() ); | |
48 } | |
49 return tbl; | |
50 } | |
51 | |
52 private boolean matchPropertyChar() { | |
53 return parser.inCharRange('a','z') | |
54 || parser.inCharRange('A','Z') | |
55 || parser.inCharRange('0','9') | |
56 || parser.anyOf("_-") | |
57 ; | |
58 } | |
59 | |
60 private boolean matchSpace() { | |
61 return parser.anyOf(" \t\r\n"); | |
62 } | |
63 | |
64 } |