annotate src/luan/modules/parsers/Css.java @ 1289:8d54bcc0b6d3

add css style parser
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 31 Dec 2018 20:59:08 -0700
parents
children f41919741100
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1289
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1 package luan.modules.parsers;
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
2
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
3 import luan.LuanState;
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
4 import luan.LuanTable;
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
5 import luan.lib.parser.Parser;
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
6
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
7
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
8 public final class Css {
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10 public static LuanTable style(LuanState luan,String text) {
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11 return new Css(luan,text).parseStyle();
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
12 }
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
14 private final LuanState luan;
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15 private final Parser parser;
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17 private Css(LuanState luan,String text) {
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18 this.luan = luan;
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19 this.parser = new Parser(text);
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20 }
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22 private LuanTable parseStyle() {
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23 LuanTable tbl = new LuanTable(luan);
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24 while( matchSpace() );
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25 while( !parser.endOfInput() ) {
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26 int start = parser.currentIndex();
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27 if( !matchPropertyChar() )
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28 return null;
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29 while( matchPropertyChar() );
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
30 String prop = parser.textFrom(start).toLowerCase();
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
31
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
32 while( matchSpace() );
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
33 if( !parser.match(':') )
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
34 return null;
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
35
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
36 start = parser.currentIndex();
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
37 while( !parser.endOfInput() && parser.noneOf(";") );
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
38 String val = parser.textFrom(start).trim();
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
39
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
40 tbl.rawPut(prop,val);
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
41 parser.match(';');
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
42 while( matchSpace() );
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
43 }
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
44 return tbl;
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
45 }
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
46
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
47 private boolean matchPropertyChar() {
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
48 return parser.inCharRange('a','z')
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
49 || parser.inCharRange('A','Z')
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
50 || parser.inCharRange('0','9')
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
51 || parser.anyOf("_-")
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
52 ;
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
53 }
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
54
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
55 private boolean matchSpace() {
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
56 return parser.anyOf(" \t\r\n");
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
57 }
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
58
8d54bcc0b6d3 add css style parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
59 }