Created
March 24, 2018 02:30
-
-
Save andiogenes/4886b4237e85001deaa91643396220a8 to your computer and use it in GitHub Desktop.
Parser which represents string of s-expressions as list of strings and nested lists
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import js.Browser; | |
import js.html.ButtonElement; | |
import js.html.TextAreaElement; | |
/** | |
* @author Anton | |
* s-expressions parser | |
*/ | |
class Test | |
{ | |
public function new() | |
{ | |
var _in : TextAreaElement = Browser.document.createTextAreaElement(); | |
var _out : TextAreaElement = Browser.document.createTextAreaElement(); | |
var _button : ButtonElement = Browser.document.createButtonElement(); | |
_in.style.width = _in.style.height = _out.style.width = _out.style.height = "250px"; | |
_in.textContent = "(+ 5 (* 3 5) 5)\n(+ 5 6)"; | |
_button.textContent = "Click"; | |
Browser.document.body.appendChild(_in); | |
Browser.document.body.appendChild(_out); | |
Browser.document.body.appendChild(_button); | |
var pars = new SExpParser(_in.textContent); | |
_button.onclick = function(e) { | |
pars.expression = _in.value; | |
var parsed = pars.parse(); | |
_out.textContent = parsed.toString(); | |
trace(parsed); | |
} | |
} | |
static function main() | |
{ | |
new Test(); | |
} | |
} | |
class SExpParser | |
{ | |
public var expression : String; | |
public function new(expression : String) | |
{ | |
this.expression = expression; | |
} | |
public function parse() : List<Dynamic> | |
{ | |
var list : List<Dynamic> = new List<Dynamic>(); | |
list.add(new List<Dynamic>()); | |
var isStringEntry = false; | |
var isSymbolEntry = false; | |
var dataString = ""; | |
for (i in 0...expression.length) | |
{ | |
switch (expression.charAt(i)) | |
{ | |
case '(': | |
if (isStringEntry) | |
dataString += '('; | |
else | |
list.add(new List<Dynamic>()); | |
case ')': | |
if (isStringEntry) | |
dataString += ')'; | |
else | |
{ | |
if (dataString != "") | |
{ | |
list.last().add(dataString); | |
dataString = ""; | |
} | |
if (isSymbolEntry) isSymbolEntry = false; | |
var temp = list.last(); | |
list.remove(list.last()); | |
list.last().add(temp); | |
} | |
case '"': | |
if (isStringEntry) | |
{ | |
dataString += '"'; | |
list.last().add(dataString); | |
dataString = ""; | |
isStringEntry = false; | |
} | |
else | |
{ | |
isStringEntry = true; | |
dataString = '"'; | |
} | |
case " ", "\n", "\t": | |
if (isStringEntry) | |
dataString += ' '; | |
else | |
{ | |
if (isSymbolEntry) | |
{ | |
list.last().add(dataString); | |
dataString = ""; | |
isSymbolEntry = false; | |
} | |
} | |
default: | |
if (!isSymbolEntry) isSymbolEntry = true; | |
dataString += expression.charAt(i); | |
} | |
} | |
return list.first(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment