Navigation Menu

Skip to content

Commit

Permalink
initial
Browse files Browse the repository at this point in the history
  • Loading branch information
Simn committed Apr 16, 2012
0 parents commit 7a11665
Show file tree
Hide file tree
Showing 19 changed files with 2,197 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .gitignore
@@ -0,0 +1,7 @@

/bin/php
/bin/js.js
/bin/neko.n
/bin/swf8.swf
/bin/swf9.swf
/bin/cpp
11 changes: 11 additions & 0 deletions bin/js.html
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<div id="haxe:trace"></div>
<script src="js.js"></script>
</body>
</html>
23 changes: 23 additions & 0 deletions build.hxml
@@ -0,0 +1,23 @@
build_base.hxml
-neko bin/neko.n

--next
build_base.hxml
-swf bin/swf9.swf

--next
build_base.hxml
-swf-version 8
-swf bin/swf8.swf

--next
build_base.hxml
-php bin/php

--next
build_base.hxml
-js bin/js.js

--next
build_base.hxml
-cpp bin/cpp
4 changes: 4 additions & 0 deletions build_base.hxml
@@ -0,0 +1,4 @@
-cp src
-cp test
-lib tink_macros
-main TestMain
56 changes: 56 additions & 0 deletions selecthxml.hxproj
@@ -0,0 +1,56 @@
<?xml version="1.0" encoding="utf-8"?>
<project version="2">
<!-- Output SWF options -->
<output>
<movie outputType="CustomBuild" />
<movie input="" />
<movie path="bin\swf9.swf" />
<movie fps="30" />
<movie width="800" />
<movie height="600" />
<movie version="9" />
<movie minorVersion="0" />
<movie platform="Flash Player" />
<movie background="#FFFFFF" />
</output>
<!-- Other classes to be compiled into your SWF -->
<classpaths>
<class path="src" />
<class path="test" />
</classpaths>
<!-- Build options -->
<build>
<option directives="" />
<option flashStrict="False" />
<option mainClass="TestMain" />
<option enabledebug="False" />
<option additional="" />
</build>
<!-- haxelib libraries -->
<haxelib>
<library name="tink_macros" />
</haxelib>
<!-- Class files to compile (other referenced classes will automatically be included) -->
<compileTargets>
<!-- example: <compile path="..." /> -->
</compileTargets>
<!-- Assets to embed into the output SWF -->
<library>
<!-- example: <asset path="..." id="..." update="..." glyphs="..." mode="..." place="..." sharepoint="..." /> -->
</library>
<!-- Paths to exclude from the Project Explorer tree -->
<hiddenPaths>
<!-- example: <hidden path="..." /> -->
</hiddenPaths>
<!-- Executed before build -->
<preBuildCommand>haxe build.hxml</preBuildCommand>
<!-- Executed after build -->
<postBuildCommand alwaysRun="False" />
<!-- Other project options -->
<options>
<option showHiddenPaths="False" />
<option testMovie="Default" />
</options>
<!-- Plugin storage -->
<storage />
</project>
84 changes: 84 additions & 0 deletions src/selecthxml/SelectDom.hx
@@ -0,0 +1,84 @@
package selecthxml;

import selecthxml.engine.Parser;
import selecthxml.engine.Type;
import selecthxml.engine.XmlDom;
import selecthxml.TypedXml;

#if macro

import selecthxml.engine.MacroHelper;
import haxe.macro.Context;
import haxe.macro.Expr;

#else

import selecthxml.engine.RegexLexer;
import selecthxml.engine.SelectEngine;

#end

class SelectDom
{
#if !macro @:macro #end
static public function select<T>(xml:ExprRequire<TypedXml<T>>, selectionString:String)
{
if (selectionString == null || selectionString.length == 0)
return Context.error("Selection string expected.", xml.pos);

return MacroHelper.makeFunction(xml, selectionString);
}

@:allowConstraint static public inline function getXml<T:Xml>(result:TypedResult<T>):T
return untyped result.__x_m_l__

#if !macro

static var lastXmlDom:XmlDom;
static var selectorCache = new Hash<Selector>();

static public function runtimeSelect(xml:Xml, selector:String)
{
if (lastXmlDom == null || lastXmlDom.xml != xml)
lastXmlDom = new XmlDom(xml);

var xmlDom = lastXmlDom;

if (!selectorCache.exists(selector))
{
var lexer = new RegexLexer(selector);
var parser = new Parser(lexer);
selectorCache.set(selector, parser.parse());
}

var s = selectorCache.get(selector);

if (isIdOnly(s))
{
var dom = xmlDom.getElementById(s[0].id);
if (dom == null) return [];
return [dom.xml];
}

var engine = new SelectEngine();
var result = engine.query(s, xmlDom);
var ret = [];
for (r in result)
ret.push(cast(r, XmlDom).xml);
return ret;
}

static inline function isIdOnly(s:Selector):Bool
{
var p = s[0];
return s.length == 1
&& p.id != null
&& p.tag == null
&& p.classes.length == 0
&& p.attrs.length == 0
&& p.pseudos.length == 0
&& p.combinator == null;
}

#end
}
11 changes: 11 additions & 0 deletions src/selecthxml/TypedResult.hx
@@ -0,0 +1,11 @@
package selecthxml;

class TypedResult<T> implements Dynamic
{
var __x_m_l__:Xml;

public function new(xml:Xml)
{
__x_m_l__ = xml;
}
}
3 changes: 3 additions & 0 deletions src/selecthxml/TypedXml.hx
@@ -0,0 +1,3 @@
package selecthxml;

typedef TypedXml<T> = Xml;
131 changes: 131 additions & 0 deletions src/selecthxml/engine/Lexer.hx
@@ -0,0 +1,131 @@
package selecthxml.engine;
import selecthxml.engine.Type;
import haxe.io.Input;

class Lexer {
var input:Input;
var currentPos:Int;
var buffer:Array<Int>;

public function new(input:Input) {
this.input = input;
buffer = [];
currentPos = 1;
}

public function readToken():Token {
var min = currentPos;
var token = readTokenDef();
var max = currentPos;
return { def: token, pos: {min: min, max: max} }
}

public function readTokenDef():TokenDef {
var c = readChar();
switch(c) {
case 0: return TEof;
case ".".code: return TDot;
case "#".code: return THash;
case ":".code: return TColon;
case "*".code: return TAsterisk;
case "[".code: return TSquareBrkOpen;
case "]".code: return TSquareBrkClose;
case "(".code: return TParenOpen;
case ")".code: return TParenClose;
case "\"".code: return readString("\"".code);
case "'".code: return readString("'".code);
case ">".code: return TGt;
case "+".code: return TPlus;
case "-".code: return TMinus;
case "~".code: return TTilde;
case "^".code: return TCaret;
case "$".code: return TDollar;
case "|".code: return TPipe;
case "=".code: return TEquals;
case "_".code: return TUnderscore;
default:
// Alpha a-zA-Z
if (isAlpha(c)) {
var str = "";
while (true) {
str += String.fromCharCode(c);
c = readChar();
if (!isAlpha(c)) {
pushChar(c);
break;
}
}
return TAlpha(str);
}
// Number 0-9
else if (isNumber(c)) {
var str = "";
while (true) {
str += String.fromCharCode(c);
c = readChar();
if (!isNumber(c)) {
pushChar(c);
break;
}
}
return TInteger(Std.parseInt(str));
}
else if (isWhitespace(c)) {
while (true) {
c = readChar();
if (!isWhitespace(c)) {
pushChar(c);
break;
}
}
return TWhitespace;
}
throw EInvalidCharacter(String.fromCharCode(c), { min: currentPos - 1, max: currentPos });
return null;
}
}

function readString(quoteType:Int) {
var str = "";
var esc = false;
var startPos = currentPos;
while (true) {
var c = readChar();
if (esc == true) {
esc = false;
if (c == quoteType) {
str += String.fromCharCode(c);
}
}
else if (c == "\\".code) esc = true;
else if (c == quoteType) break;
else if (c == 0) throw EUnterminatedString({ min:startPos - 1, max:startPos });
else str += String.fromCharCode(c);
}
return TString(str);
}

inline function isAlpha(c:Int) {
return (c >= "A".code && c <= "Z".code) || (c >= "a".code && c <= "z".code);
}

inline function isNumber(c:Int) {
return c >= "0".code && c <= "9".code;
}

inline function isWhitespace(c:Int) {
return c == 32 || c == 9 || c == 13;
}

function readChar():Int {
currentPos++;
if (buffer.length > 0)
return buffer.shift();
try { return input.readByte(); } catch (ex:Dynamic) { return 0; }
}

function pushChar(char:Int):Void {
currentPos--;
buffer.push(char);
}
}

0 comments on commit 7a11665

Please sign in to comment.