| 1 |
# Treetop grammar for simple cases of font cfg files) |
| 2 |
grammar FrogattoFontCfg |
| 3 |
rule file |
| 4 |
space* '[font]' space* attributes space* chars_list:(chars space*)* '[/font]' space* { |
| 5 |
def character_widths |
| 6 |
result = {} |
| 7 |
chars_list.elements.each do |element| |
| 8 |
chars = element.chars |
| 9 |
chars.characters.each do |character| |
| 10 |
result[character] = chars.width |
| 11 |
end |
| 12 |
end |
| 13 |
result |
| 14 |
end |
| 15 |
} |
| 16 |
end |
| 17 |
|
| 18 |
rule chars |
| 19 |
'[chars]' space* attributes '[/chars]' { |
| 20 |
def characters |
| 21 |
attributes.values['chars'].to_s.chars |
| 22 |
end |
| 23 |
|
| 24 |
def width |
| 25 |
attributes.values['width'] || attributes.values['rect'] |
| 26 |
end |
| 27 |
} |
| 28 |
end |
| 29 |
|
| 30 |
rule attributes |
| 31 |
(attribute space*)* { |
| 32 |
def values |
| 33 |
result = {} |
| 34 |
elements.each do |element| |
| 35 |
result[element.attribute.name] = element.attribute.value |
| 36 |
end |
| 37 |
result |
| 38 |
end |
| 39 |
} |
| 40 |
end |
| 41 |
|
| 42 |
rule attribute |
| 43 |
word '=' expression { |
| 44 |
def name |
| 45 |
word.text_value |
| 46 |
end |
| 47 |
|
| 48 |
def value |
| 49 |
expression.value |
| 50 |
end |
| 51 |
} |
| 52 |
end |
| 53 |
|
| 54 |
rule expression |
| 55 |
rect / |
| 56 |
integer / |
| 57 |
string |
| 58 |
end |
| 59 |
|
| 60 |
rule string |
| 61 |
'"\""' { |
| 62 |
def value |
| 63 |
'"' |
| 64 |
end |
| 65 |
} / |
| 66 |
'"' body:[^"]* '"' { |
| 67 |
def value |
| 68 |
body.text_value |
| 69 |
end |
| 70 |
} / |
| 71 |
"'" body:[^']* "'" { |
| 72 |
def value |
| 73 |
body.text_value |
| 74 |
end |
| 75 |
} / |
| 76 |
word |
| 77 |
end |
| 78 |
|
| 79 |
rule word |
| 80 |
[^= \n]+ { |
| 81 |
def value |
| 82 |
text_value |
| 83 |
end |
| 84 |
} |
| 85 |
end |
| 86 |
|
| 87 |
rule rect |
| 88 |
left:integer ',' top:integer ',' right:integer ',' bottom:integer { |
| 89 |
# we only care about width |
| 90 |
def value |
| 91 |
right.value - left.value |
| 92 |
end |
| 93 |
} |
| 94 |
end |
| 95 |
|
| 96 |
rule integer |
| 97 |
[0-9]+ { |
| 98 |
def value |
| 99 |
Integer(text_value) |
| 100 |
end |
| 101 |
} |
| 102 |
end |
| 103 |
|
| 104 |
rule space |
| 105 |
' ' / "\t" / "\n" / '#' [^\n]* "\n" |
| 106 |
end |
| 107 |
end |
| 108 |
|