|
| 1 | +{ |
| 2 | +// Go code goes here... |
| 3 | + |
| 4 | +} |
| 5 | + |
| 6 | + |
| 7 | +Input <- forms:Form* EOF { |
| 8 | + return forms, nil |
| 9 | +} |
| 10 | + |
| 11 | +Form <- _ form:( LineComment / List / Lambda / Vector / Set / Map / |
| 12 | + QuotedForm / Numeric / Symbol / Keyword / String |
| 13 | + ) _ { |
| 14 | + fmt.Printf("Form: %v\n", form) |
| 15 | + return string(c.text), nil |
| 16 | +} |
| 17 | + |
| 18 | +EOF = !. |
| 19 | +EOL <- LineComment? ( "\r\n" / "\n\r" / "\r" / "\n" / EOF) |
| 20 | +_ "whitespace" <- [ \n\t\r,]* |
| 21 | + |
| 22 | +LineComment <- _? ';' comment:[^\r\n]* { |
| 23 | + return &Comment{ |
| 24 | + Position: Position{Line: c.pos.line, Column: c.pos.col, Offset: c.pos.offset}, |
| 25 | + Value: fmt.Sprint(comment), |
| 26 | + }, nil |
| 27 | + /* return showComponent("commented-line", c.text) */ |
| 28 | +} |
| 29 | + |
| 30 | +List <- '(' forms:Form* ')' { |
| 31 | + return &Collection{ |
| 32 | + Position: Position{Line: c.pos.line, Column: c.pos.col, Offset: c.pos.offset}, |
| 33 | + Type: CollectionList, |
| 34 | + Items: forms.([]interface{}), |
| 35 | + }, nil |
| 36 | +} |
| 37 | + |
| 38 | +Vector <- '[' forms:Form* ']' { |
| 39 | + return &Collection{ |
| 40 | + Position: Position{Line: c.pos.line, Column: c.pos.col, Offset: c.pos.offset}, |
| 41 | + Type: CollectionVector, |
| 42 | + Items: forms.([]interface{}), |
| 43 | + }, nil |
| 44 | +} |
| 45 | + |
| 46 | +Map <- '{' forms:Form* '}' { |
| 47 | + return &Collection{ |
| 48 | + Position: Position{Line: c.pos.line, Column: c.pos.col, Offset: c.pos.offset}, |
| 49 | + Type: CollectionMap, |
| 50 | + Items: forms.([]interface{}), |
| 51 | + }, nil |
| 52 | +} |
| 53 | + |
| 54 | +Set <- '#' '{' forms:Form '}' { |
| 55 | + return &Collection{ |
| 56 | + Position: Position{Line: c.pos.line, Column: c.pos.col, Offset: c.pos.offset}, |
| 57 | + Type: CollectionSet, |
| 58 | + Items: forms.([]interface{}), |
| 59 | + }, nil |
| 60 | +} |
| 61 | + |
| 62 | +Lambda <- '#' '(' forms:Form* ')' { |
| 63 | + return &Collection{ |
| 64 | + Position: Position{Line: c.pos.line, Column: c.pos.col, Offset: c.pos.offset}, |
| 65 | + Type: CollectionLambda, |
| 66 | + Items: forms.([]interface{}), |
| 67 | + }, nil |
| 68 | +} |
| 69 | + |
| 70 | +Keyword <- ( NamespacedKeyword / BareKeyword) |
| 71 | + |
| 72 | +NamespacedKeyword <- ':' ':' sym:Symbol { |
| 73 | + return &Keyword{ |
| 74 | + Position: Position{Line: c.pos.line, Column: c.pos.col, Offset: c.pos.offset}, |
| 75 | + Name: fmt.Sprint(sym), |
| 76 | + Namespace: "TODO: namespaced", |
| 77 | + }, nil |
| 78 | +} |
| 79 | + |
| 80 | +BareKeyword <- ':' sym:Symbol { |
| 81 | + return &Keyword{ |
| 82 | + Position: Position{Line: c.pos.line, Column: c.pos.col, Offset: c.pos.offset}, |
| 83 | + Name: fmt.Sprint(sym), |
| 84 | + }, nil |
| 85 | +} |
| 86 | + |
| 87 | +Symbol <- [a-z0-9-+*?!&^%#?<>%/\\]i+ { |
| 88 | + return &Symbol{ |
| 89 | + Position: Position{Line: c.pos.line, Column: c.pos.col, Offset: c.pos.offset}, |
| 90 | + Name: string(c.text), |
| 91 | + }, nil |
| 92 | +} |
| 93 | + |
| 94 | +QuotedForm <- ( "'" ) form:Form { |
| 95 | + return &QuotedForm{ |
| 96 | + Position: Position{Line: c.pos.line, Column: c.pos.col, Offset: c.pos.offset}, |
| 97 | + Form: form, |
| 98 | + }, nil |
| 99 | +} |
| 100 | + |
| 101 | +String <- '"' value:[^"]* '"' { |
| 102 | + return &String{ |
| 103 | + Position: Position{Line: c.pos.line, Column: c.pos.col, Offset: c.pos.offset}, |
| 104 | + Value: string(c.text), |
| 105 | + }, nil |
| 106 | +} |
| 107 | + |
| 108 | +Numeric <- ( SciNot / Float /Integer ) |
| 109 | + |
| 110 | +SciNotSuffix <- [eE] Integer |
| 111 | + |
| 112 | +SciNot <- coefficient:( Float / Integer ) exponent:SciNotSuffix { |
| 113 | + fl := fmt.Sprintf("(sci-not %s :exponent %s)", coefficient, exponent) |
| 114 | + fmt.Println(fl) |
| 115 | + return fl, nil |
| 116 | +} |
| 117 | + |
| 118 | +Float <- '-'? [0-9]+ '.' [0-9]+ { |
| 119 | + value, err := strconv.ParseFloat(string(c.text), 64) |
| 120 | + if err != nil { |
| 121 | + panic(err) |
| 122 | + } |
| 123 | + return &Float{ |
| 124 | + Position: Position{Line: c.pos.line, Column: c.pos.col, Offset: c.pos.offset}, |
| 125 | + ParsedValue: string(c.text), |
| 126 | + Value: value, |
| 127 | + }, nil |
| 128 | +} |
| 129 | + |
| 130 | +Integer <- '-'? [0-9]+ { |
| 131 | + value, err := strconv.ParseInt(string(c.text), 0, 64) |
| 132 | + if err != nil { |
| 133 | + panic(err) |
| 134 | + } |
| 135 | + return &Integer{ |
| 136 | + Position: Position{Line: c.pos.line, Column: c.pos.col, Offset: c.pos.offset}, |
| 137 | + ParsedValue: string(c.text), |
| 138 | + Value: value, |
| 139 | + }, nil |
| 140 | +} |
0 commit comments