Created
March 25, 2014 22:04
-
-
Save skial/9772434 to your computer and use it in GitHub Desktop.
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
package ; | |
import byte.ByteData; | |
class Main { | |
public static function main() { | |
var l = new RandomLexer( ByteData.ofString( '<tag><hello></hello><world></world></tag>' ), 'random-pause' ); | |
var t = []; | |
try while (true) t.push( l.token( RandomLexer.root ) ) catch (e:Dynamic) trace( e ); | |
trace( t ); | |
} | |
} |
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
package ; | |
import haxe.io.Eof; | |
import hxparse.Lexer; | |
import hxparse.RuleBuilder; | |
import byte.ByteData; | |
import hxparse.Ruleset.Ruleset; | |
enum RandomKeywords { | |
Tag(name:String, tokens:Array<RandomKeywords>); | |
} | |
class RandomLexer extends Lexer implements RuleBuilder { | |
public function new(content:ByteData, name:String) { | |
super( content, name ); | |
} | |
public static var tags = @:rule [ | |
' ' => Tag('', []), | |
'<[^/][a-zA-Z0-9]+>' => { | |
var current = lexer.current; | |
var tag = current.substring(1, current.length - 1); | |
trace( tag ); | |
var inner = lexer.token( closing( tag ) ); | |
trace( inner ); | |
var parsed = parse( inner, tag ); | |
Tag(tag, parsed); | |
}, | |
]; | |
public static function closing(tag:String) { | |
var copy = tag.substring(0); | |
return Lexer.buildRuleset([{ | |
rule:'.*</$copy>', | |
func:function(lexer:Lexer) { | |
var c = lexer.current; | |
return c.substring(0, c.length - '</$copy>'.length); | |
}, | |
}]); | |
} | |
public static var root = tags; | |
public static function parse(value:String, name:String) { | |
var lexer = new RandomLexer( ByteData.ofString( value ), '$name-inner' ); | |
var tokens = []; | |
try { | |
while (true) { | |
tokens.push( lexer.token( tags ) ); | |
} | |
} catch (e:Eof) if (lexer.pos != lexer.input.length) { | |
tokens = tokens.concat( parse( value.substring(lexer.pos), name ) ); | |
} catch (e:Dynamic) { | |
} | |
return tokens; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment