text:
use std::error::Error;

use base64::encode;
use yaml::yaml::Hash;
use yaml::{EmitError, Yaml, YamlEmitter, YamlLoader};

use parameter::{ParamMap, Parameter, ParameterValues};
use processor::process_yaml;
use secret::{Secret, Secrets};

/// A Kubernetes manifest template and the values for each of its parameters.
#[derive(Debug)]
pub struct Template {
    objects: Vec<Yaml>,
    param_map: ParamMap,
    secrets: Option<Secrets>,
}

impl Template {
    /// Creates a new template.
    ///
    /// # Parameters
    ///
    /// * template_contents: The YAML template file's contents.
    /// * parameter_values: A map of the template's parameters and the user-supplied values for
    ///   each.
    /// * secrets: A list of Kubernetes secrets whose data keys should be Base64 encoded after
    ///   parameter interpolation.
    ///
    /// # Errors
    ///
    /// Returns an error if:
    ///
    /// * There was more than one YAML document present in the template contents.
    /// * The YAML document did not contain an "objects" key or it was not an array value.
    /// * The YAML document did not contain a "parameters" key or it was not an array value.
    /// * One of the parameters doesn't have a "name" key.
    /// * One of the parameters specifies an invalid "parameterType".
    /// * One of the parameters requires a value which wasn't supplied.
    /// * Any of the provided secrets were not found in the template.
    /// * There was an error in the structure of a secret that prevented its data from being Base64
    /// encoded.
    pub fn new(
        template_contents: String,
        parameter_values: ParameterValues,
        secrets: Option<Secrets>,
    ) -> Result<Self, String> {
        let docs = YamlLoader::load_from_str(&template_contents)
            .map_err(|err| err.description().to_owned())?;

        if docs.len() != 1 {
            return Err("Only one YAML document can be present in the template.".to_owned());
        }

        let doc = &docs[0];

        let mut template_objects = vec![];
        let objects = match doc["objects"].as_vec() {
            Some(objects) => objects,
            None => return Err("Key \"objects\" must be present and must be an array.".to_owned())
        };

        for object in objects {
            template_objects.push(object.clone());
        }

        let mut param_map = ParamMap::new();
        let parameter_specs = match doc["parameters"].as_vec() {
            Some(parameter_specs) => parameter_specs,
            None => return Err("Key \"parameters\" must be present and must be an array.".to_owned())
        };

        for parameter_spec in parameter_specs {
            let parameter = Parameter::new(parameter_spec, &parameter_values)?;

            param_map.insert(parameter.name.clone(), parameter);
        }

        Ok(Template {
            objects: template_objects,
            param_map: param_map,
            secrets: secrets,
        })
    }

    /// Interpolates the parameters' values into the YAML template, returning the results.
    ///
    /// # Errors
    ///
    /// Returns an error if the processed template was not valid YAML, or if any specified secrets
    /// could not be found and Base64 encoded.
    pub fn process(mut self) -> Result<String, String> {
        let mut secrets_encoded = 0;

        for object in self.objects.iter_mut() {
            process_yaml(object, &self.param_map);

            if let Some(ref secrets) = self.secrets {
                if maybe_base64_encode_secret(secrets, object)? {
                    secrets_encoded += 1;
                }
            }
        }

        if let Some(ref secrets) = self.secrets {
            if secrets_encoded != secrets.len() {
                return Err("Not all secrets specified were found.".to_string());
            }
        }

        dump(self.objects)
    }

}

fn maybe_base64_encode_secret(secrets: &Secrets, object: &mut Yaml)
-> Result<bool, String> {
    let mut hash = match object {
        &mut Yaml::Hash(ref mut hash) => hash,
        _ => return Ok(false),
    };

    if let Some(kind) = hash.get(&Yaml::String("kind".to_string())) {
        match kind {
            &Yaml::String(ref kind_string) => {
                if kind_string != "Secret" {
                    return Ok(false);
                }
            }
            _ => return Err(
                "Encountered a resource with a non-string value for the \"kind\" field.".to_string()
            ),
        }
    } else {
        return Err("Encountered a resource without a \"kind\" field.".to_string());
    }

    let metadata = match hash.get(&Yaml::String("metadata".to_string())) {
        Some(&Yaml::Hash(ref metadata)) => metadata.clone(),
        Some(_) => return Err(
            "Encountered a resource with a non-hash \"metadata\" field.".to_string()
        ),
        None => return Err(
            "Encountered a resource without a \"metadata\" field.".to_string()
        ),
    };

    let name = match metadata.get(&ystring("name")) {
        Some(&Yaml::String(ref name)) => name.to_string(),
        Some(_) => return Err(
            "Encountered a resource with a non-string \"metadata.name\" field.".to_string()
        ),
        None => return Err(
            "Encountered a resource without a \"metadata.name\" field.".to_string()
        ),
    };

    let namespace = match metadata.get(&ystring("namespace")) {
        Some(&Yaml::String(ref namespace)) => namespace.to_string(),
        Some(_) => return Err(
            "Encountered a resource with a non-string \"metadata.namespace\" field.".to_string()
        ),
        None => "default".to_string(),
    };

    let secret = Secret {
        name: name,
        namespace: namespace,
    };

    if secrets.contains(&secret) {
        if let Some(data) = hash.get_mut(&ystring("data")) {
            match data {
                &mut Yaml::Hash(ref mut data_hash) => {
                    base64_encode_secret_data(data_hash)?;
                    return Ok(true);
                }
                _ => return Err(
                    "Encountered secret with non-hash \"data\" field.".to_string()
                ),
            }
        }
    }

    return Ok(false);
}
fn base64_encode_secret_data(data: &mut Hash) -> Result<(), String> {
    for (_, value) in data.iter_mut() {
        let encoded = match value {
            &mut Yaml::String(ref value_string) => encode(value_string.as_bytes()),
            _ => return Err("Encountered non-string secret data value.".to_string()),
        };

        *value = ystring(&encoded);
    }

    Ok(())
}

fn dump(objects: Vec<Yaml>) -> Result<String, String> {
    let mut manifests = String::new();
    let last = objects.len() - 1;

    for (i, object) in objects.iter().enumerate() {
        {
            let mut emitter = YamlEmitter::new(&mut manifests);
            emitter.dump(&object).map_err(|error| {
                match error {
                    EmitError::FmtError(error) => format!("{}", error),
                    EmitError::BadHashmapKey => "Bad hashmap key in YAML structure.".to_owned(),
                }
            })?;
        }

        if i != last {
            manifests.push_str("\n");
        }
    }

    Ok(manifests)
}

fn ystring(s: &str) -> Yaml {
    Yaml::String(s.to_string())
}


text:
<resources>
    <string name="app_name">wenweather</string>
</resources>

redpajama_set_name: RedPajamaGithub


text:
// Jison, an LR(0), SLR(1), LARL(1), LR(1) Parser Generator
// Zachary Carter <zach@carter.name>
// MIT X Licensed

if (typeof exports === 'undefined') {
    exports = {};
} else if (typeof require !== 'undefined') {
    // assume we're in commonjs land
    //var system = require("system");
    var typal = require('./jison/util/typal').typal;
    var Set = require('./jison/util/set').Set;
    var RegExpLexer = require('./jison/lexer').RegExpLexer;
}

var Jison = exports.Jison = exports;

// detect print
if (typeof puts !== 'undefined') {
    Jison.print = function print () { puts([].join.call(arguments, ' ')); };
} else if (typeof print !== 'undefined') {
    Jison.print = print;
} else {
    Jison.print = function print () {};
}

Jison.Parser = (function () {

// iterator utility
function each (obj, func) {
    if (obj.forEach) {
        obj.forEach(func);
    } else {
        var p;
        for (p in obj) {
            if (obj.hasOwnProperty(p)) {
                func.call(obj, obj[p], p, obj);
            }
        }
    }
}

var Nonterminal = typal.construct({
    constructor: function Nonterminal (symbol) {
        this.symbol = symbol;
        this.productions = new Set();
        this.first = [];
        this.follows = [];
        this.nullable = false;
    },
    toString: function Nonterminal_toString () {
        var str = this.symbol+"\n";
        str += (this.nullable ? 'nullable' : 'not nullable');
        str += "\nFirsts: "+this.first.join(', ');
        str += "\nFollows: "+this.first.join(', ');
        str += "\nProductions:\n  "+this.productions.join('\n  ');

        return str;
    }
});

var Production = typal.construct({
    constructor: function Production (symbol, handle, id) {
        this.symbol = symbol;
        this.handle = handle;
        this.nullable = false;
        this.id = id;
        this.first = [];
        this.precedence = 0;
    },
    toString: function Production_toString () {
        return this.symbol+" -> "+this.handle.join(' ');
    }
});

var generator = typal.beget();

generator.constructor = function Jison_Generator (grammar, opt) {
    if (typeof grammar === 'string') {
        grammar = require("jison/bnf").parse(grammar);
    }

    var options = typal.mix.call({}, grammar.options, opt);
    this.terms = {};
    this.operators = {};
    this.productions = [];
    this.conflicts = 0;
    this.resolutions = [];
    this.options = options;
    this.yy = {}; // accessed as yy free variable in the parser/lexer actions

    // source included in semantic action execution scope
    if (grammar.actionInclude) {
        if (typeof grammar.actionInclude === 'function') {
            grammar.actionInclude = String(grammar.actionInclude).replace(/^\s*function \(\) \{/, '').replace(/\}\s*$/, '');
        }
        this.actionInclude = grammar.actionInclude;
    }

    this.DEBUG = options.debug || false;
    if (this.DEBUG) this.mix(generatorDebug); // mixin debug methods

    this.processGrammar(grammar);

    if (grammar.lex) {
        this.lexer = new RegExpLexer(grammar.lex, null, this.terminals_);
    }
};

generator.processGrammar = function processGrammarDef (grammar) {
    var bnf = grammar.bnf,
        tokens = grammar.tokens,
        nonterminals = this.nonterminals = {},
        productions = this.productions,
        self = this;

    if (tokens) {
        if (typeof tokens === 'string') {
            tokens = tokens.trim().split(' ');
        } else {
            tokens = tokens.slice(0);
        }
    }

    var symbols = this.symbols = [];

    // calculate precedence of operators
    var operators = this.operators = processOperators(grammar.operators);

    // build productions from cfg
    this.buildProductions(grammar.bnf, productions, nonterminals, symbols, operators);

    if (tokens && this.terminals.length !== tokens.length) {
        self.trace("Warning: declared tokens differ from tokens found in rules.");
        self.trace(this.terminals);
        self.trace(tokens);
    }

    // augment the grammar
    this.augmentGrammar(grammar);
};

generator.augmentGrammar = function augmentGrammar (grammar) {
    // use specified start symbol, or default to first user defined production
    this.startSymbol = grammar.start || grammar.startSymbol || this.productions[0].symbol;
    if (!this.nonterminals[this.startSymbol]) {
        throw new Error("Grammar error: startSymbol must be a non-terminal found in your grammar.");
    }
    this.EOF = "$end";

    // augment the grammar
    var acceptProduction = new Production('$accept', [this.startSymbol, '$end'], 0);
    this.productions.unshift(acceptProduction);

    // prepend parser tokens
    this.symbols.unshift("$accept",this.EOF);
    this.symbols_["$accept"] = 0;
    this.symbols_[this.EOF] = 1;
    this.terminals.unshift(this.EOF);

    this.nonterminals["$accept"] = new Nonterminal("$accept");
    this.nonterminals["$accept"].productions.push(acceptProduction);

    // add follow $ to start symbol
    this.nonterminals[this.startSymbol].follows.push(this.EOF);
};

// set precedence and associativity of operators
function processOperators (ops) {
    if (!ops) return {};
    var operators = {};
    for (var i=0,k,prec;prec=ops[i]; i++) {
        for (k=1;k < prec.length;k++) {
            operators[prec[k]] = {precedence: i+1, assoc: prec[0]};
        }
    }
    return operators;
}

generator.buildProductions = function buildProductions(bnf, productions, nonterminals, symbols, operators) {
    var actions = [this.actionInclude || "", "var $$ = arguments[5],$0=arguments[5].length;",'switch(arguments[4]) {'],
        prods, symbol;
    var productions_ = [0];
    var symbolId = 1;
    var symbols_ = {};

    var her = false; // has error recovery

    function addSymbol (s) {
        if (s && !symbols_[s]) {
            symbols_[s] = ++symbolId;
            symbols.push(s);
        }
    }

    // add error symbol; will be third symbol, or "2" ($accept, $end, error)
    addSymbol("error");

    for (symbol in bnf) {
        if (!bnf.hasOwnProperty(symbol)) continue;

        addSymbol(symbol);
        nonterminals[symbol] = new Nonterminal(symbol);

        if (typeof bnf[symbol] === 'string') {
            prods = bnf[symbol].split(/\s*\|\s*/g);
        } else {
            prods = bnf[symbol].slice(0);
        }

        prods.forEach(function buildProds_forEach (handle) {
            var r, rhs, i;
            if (handle.constructor === Array) {
                if (typeof handle[0] === 'string')
                    rhs = handle[0].trim().split(' ');
                else
                    rhs = handle[0].slice(0);

                for (i=0; her = her || rhs[i] === 'error',i<rhs.length; i++) if (!symbols_[rhs[i]]) {
                    addSymbol(rhs[i]);
                }

                if (typeof handle[1] === 'string' || handle.length == 3) {
                    // semantic action specified
                    var action = 'case '+(productions.length+1)+':'+handle[1]+'\nbreak;';

                    // replace named semantic values ($nonterminal)
                    if (action.match(/\$[a-zA-Z][a-zA-Z0-9_]*/)) {
                        var count = {},
                            names = {};
                        for (i=0;i<rhs.length;i++) {
                            if (names[rhs[i]]) {
                                names[rhs[i]+(++count[rhs[i]])] = i+1;
                            } else {
                                names[rhs[i]] = i+1;
                                names[rhs[i]+"1"] = i+1;
                                count[rhs[i]] = 1;
                            }
                        }
                        action = action.replace(/\$([a-zA-Z][a-zA-Z0-9_]*)/g, function (str, pl) {
                                return names[pl] ? '$'+names[pl] : pl;
                        });
                    }
                    action = action.replace(/\$(?:0|\$)/g, "this.$")
                        .replace(/\$(\d+)/g, "$$$[\$0-"+rhs.length+"+$1-1]");
                    actions.push(action);

                    r = new Production(symbol, rhs, productions.length+1);
                    // precedence specified also
                    if (handle[2] && operators[handle[2].prec]) {
                        r.precedence = operators[handle[2].prec].precedence;
                    }
                } else {
                    // only precedence specified
                    r = new Production(symbol, rhs, productions.length+1);
                    if (operators[handle[1].prec]) {
                        r.precedence = operators[handle[1].prec].precedence;
                    }
                }
            } else {
                rhs = handle.trim().split(' ');
                for (i=0; her = her || rhs[i] === 'error',i<rhs.length; i++) if (!symbols_[rhs[i]]) {
                    addSymbol(rhs[i]);
                }
                r = new Production(symbol, rhs, productions.length+1);
            }
            if (r.precedence === 0) {
                // set precedence
                for (i=r.handle.length-1; i>=0; i--) {
                    if (!(r.handle[i] in nonterminals) && r.handle[i] in operators) {
                        r.precedence = operators[r.handle[i]].precedence;
                    }
                }
            }

            productions.push(r);
            productions_.push([symbols_[r.symbol], r.handle[0] === '' ? 0 : r.handle.length]);
            nonterminals[symbol].productions.push(r);
        });
    }

    var sym, terms = [], terms_ = {};
    each(symbols_, function (id, sym) {
        if (!nonterminals[sym]) {
            terms.push(sym);
            terms_[id] = sym;
        }
    });

    this.hasErrorRecovery = her;

    this.terminals = terms;
    this.terminals_ = terms_;
    this.symbols_ = symbols_;

    this.productions_ = productions_;
    actions.push('}');
    this.performAction = Function("yytext","yyleng","yylineno","yy", actions.join("\n"));
};

generator.createParser = function createParser () {
    throw 'Calling abstract method.';
};

// noop. implemented in debug mixin
generator.trace = function trace () { };

generator.warn = function warn () {
    Jison.print.apply(null,arguments);
};

generator.error = function error (msg) {
    throw msg;
};

// Generator debug mixin

var generatorDebug = {
    trace: function trace () {
        Jison.print.apply(null, arguments);
    },
    beforeprocessGrammar: function () {
        this.trace("Processing grammar.");
    },
    afteraugmentGrammar: function () {
        var trace = this.trace;
        each(this.symbols, function (sym, i) {
            trace(sym+"("+i+")");
        });
    }
};

/*
 * Mixin for common behaviors of lookahead parsers
 * */
var lookaheadMixin = {};

lookaheadMixin.computeLookaheads = function computeLookaheads () {
    if (this.DEBUG) this.mix(lookaheadDebug); // mixin debug methods

    this.computeLookaheads = function () {};
    this.nullableSets();
    this.firstSets();
    this.followSets();
};

// calculate follow sets typald on first and nullable
lookaheadMixin.followSets = function followSets () {
    var productions = this.productions,
        nonterminals = this.nonterminals,
        self = this,
        cont = true;

    // loop until no further changes have been made
    while(cont) {
        cont = false;

        productions.forEach(function Follow_prod_forEach (production, k) {
            //self.trace(production.symbol,nonterminals[production.symbol].follows);
            // q is used in Simple LALR algorithm determine follows in context
            var q;
            var ctx = !!self.go_;

            var set = [],oldcount;
            for (var i=0,t;t=production.handle[i];++i) {
                if (!nonterminals[t]) continue;

                // for Simple LALR algorithm, self.go_ checks if
                if (ctx)
                    q = self.go_(production.symbol, production.handle.slice(0, i));
                var bool = !ctx || q === parseInt(self.nterms_[t]);

                if (i === production.handle.length+1 && bool) {
                    set = nonterminals[production.symbol].follows
                } else {
                    var part = production.handle.slice(i+1);

                    set = self.first(part);
                    if (self.nullable(part) && bool) {
                        set.push.apply(set, nonterminals[production.symbol].follows);
                    }
                }
                oldcount = nonterminals[t].follows.length;
                Set.union(nonterminals[t].follows, set);
                if (oldcount !== nonterminals[t].follows.length) {
                    cont = true;
                }
            }
        });
    }
};

// return the FIRST set of a symbol or series of symbols
lookaheadMixin.first = function first (symbol) {
    // epsilon
    if (symbol === '') {
        return [];
    // RHS
    } else if (symbol instanceof Array) {
        var firsts = [];
        for (var i=0,t;t=symbol[i];++i) {
            if (!this.nonterminals[t]) {
                if (firsts.indexOf(t) === -1)
                    firsts.push(t);
            } else {
                Set.union(firsts, this.nonterminals[t].first);
            }
            if (!this.nullable(t))
                break;
        }
        return firsts;
    // terminal
    } else if (!this.nonterminals[symbol]) {
        return [symbol];
    // nonterminal
    } else {
        return this.nonterminals[symbol].first;
    }
};

// fixed-point calculation of FIRST sets
lookaheadMixin.firstSets = function firstSets () {
    var productions = this.productions,
        nonterminals = this.nonterminals,
        self = this,
        cont = true,
        symbol,firsts;

    // loop until no further changes have been made
    while(cont) {
        cont = false;

        productions.forEach(function FirstSets_forEach (production, k) {
            var firsts = self.first(production.handle);
            if (firsts.length !== production.first.length) {
                production.first = firsts;
                cont=true;
            }
        });

        for (symbol in nonterminals) {
            firsts = [];
            nonterminals[symbol].productions.forEach(function (production) {
                Set.union(firsts, production.first);
            });
            if (firsts.length !== nonterminals[symbol].first.length) {
                nonterminals[symbol].first = firsts;
                cont=true;
            }
        }
    }
};

// fixed-point calculation of NULLABLE
lookaheadMixin.nullableSets = function nullableSets () {
    var firsts = this.firsts = {},
        nonterminals = this.nonterminals,
        self = this,
        cont = true;

    // loop until no further changes have been made
    while(cont) {
        cont = false;

        // check if each production is nullable
        this.productions.forEach(function (production, k) {
            if (!production.nullable) {
                for (var i=0,n=0,t;t=production.handle[i];++i) {
                    if (self.nullable(t)) n++;
                }
                if (n===i) { // production is nullable if all tokens are nullable
                    production.nullable = cont = true;
                }
            }
        });

        //check if each symbol is nullable
        for (var symbol in nonterminals) {
            if (!this.nullable(symbol)) {
                for (var i=0,production;production=nonterminals[symbol].productions.item(i);i++) {
                    if (production.nullable)
                        nonterminals[symbol].nullable = cont = true;
                }
            }
        }
    }
};

// check if a token or series of tokens is nullable
lookaheadMixin.nullable = function nullable (symbol) {
    // epsilon
    if (symbol === '') {
        return true
    // RHS
    } else if (symbol instanceof Array) {
        for (var i=0,t;t=symbol[i];++i) {
            if (!this.nullable(t))
                return false;
        }
        return true;
    // terminal
    } else if (!this.nonterminals[symbol]) {
        return false;
    // nonterminal
    } else {
        return this.nonterminals[symbol].nullable;
    }
};

// lookahead debug mixin
var lookaheadDebug = {
    beforenullableSets: function () {
        this.trace("Computing Nullable sets.");
    },
    beforefirstSets: function () {
        this.trace("Computing First sets.");
    },
    beforefollowSets: function () {
        this.trace("Computing Follow sets.");
    },
    afterfollowSets: function () {
        var trace = this.trace;
        each(this.nonterminals, function (nt, t) {
            trace(nt, '\n');
        });
    }
};

/*
 * Mixin for common LR parser behavior
 * */
var lrGeneratorMixin = {};

lrGeneratorMixin.buildTable = function buildTable () {
    if (this.DEBUG) this.mix(lrGeneratorDebug); // mixin debug methods

    this.states = this.canonicalCollection();
    this.table = this.parseTable(this.states);
};

lrGeneratorMixin.Item = typal.construct({
    constructor: function Item(production, dot, f, predecessor) {
        this.production = production;
        this.dotPosition = dot || 0;
        this.follows = f || [];
        this.predecessor = predecessor;
        this.id = parseInt(production.id+'a'+this.dotPosition, 36);
        this.markedSymbol = this.production.handle[this.dotPosition];
    },
    remainingHandle: function () {
        return this.production.handle.slice(this.dotPosition+1);
    },
    eq: function (e) {
        return e.id === this.id;
    },
    toString: function () {
        var temp = this.production.handle.slice(0);
        temp[this.dotPosition] = '.'+(temp[this.dotPosition]||'');
        return '['+this.production.symbol+" -> "+temp.join(' ')
            +(this.follows.length === 0 ? "" : ", "+this.follows.join('/'))
            +']';
    }
});

lrGeneratorMixin.ItemSet = Set.prototype.construct({
    afterconstructor: function () {
        this.reductions = [];
        this.goes = {};
        this.edges = {};
        this.shifts = false;
        this.inadequate = false;
        this.hash_ = {};
        for (var i=this._items.length-1;i >=0;i--) {
            this.hash_[this._items[i].id] = true; //i;
        }
    },
    concat: function concat (set) {
        var a = set._items || set;
        for (var i=a.length-1;i >=0;i--) {
            this.hash_[a[i].id] = true; //i;
        }
        this._items.push.apply(this._items, a);
        return this;
    },
    push: function (item) {
        this.hash_[item.id] = true;
        return this._items.push(item);
    },
    contains: function (item) {
        return this.hash_[item.id];
    },
    valueOf: function toValue () {
        var v = this._items.map(function (a) {return a.id}).sort().join('|');
        return (this.valueOf = function toValue_inner() {return v;})();
    }
});

lrGeneratorMixin.closureOperation = function closureOperation (itemSet /*, closureSet*/) {
    var closureSet = new this.ItemSet();
    var self = this;

    var set = itemSet,
        itemQueue, syms = {};

    do {
    itemQueue = new Set();
    closureSet.concat(set);
    set.forEach(function CO_set_forEach (item) {
        var symbol = item.markedSymbol;

        // if token is a non-terminal, recursively add closures
        if (symbol && self.nonterminals[symbol]) {
            if(!syms[symbol]) {
                self.nonterminals[symbol].productions.forEach(function CO_nt_forEach (production) {
                    var newItem = new self.Item(production, 0);
                    if(!closureSet.contains(newItem))
                        itemQueue.push(newItem);
                });
                syms[symbol] = true;
            }
        } else if (!symbol) {
            // reduction
            closureSet.reductions.push(item);
            closureSet.inadequate = closureSet.reductions.length > 1 || closureSet.shifts;
        } else {
            // shift
            closureSet.shifts = true;
            closureSet.inadequate = closureSet.reductions.length > 0;
        }
    });

    set = itemQueue;

    } while (!itemQueue.isEmpty());

    return closureSet;
};

lrGeneratorMixin.gotoOperation = function gotoOperation (itemSet, symbol) {
    var gotoSet = new this.ItemSet(),
        self = this;

    itemSet.forEach(function goto_forEach(item, n) {
        if (item.markedSymbol === symbol) {
            gotoSet.push(new self.Item(item.production, item.dotPosition+1, item.follows, n));
        }
    });

    return gotoSet.isEmpty() ? gotoSet : this.closureOperation(gotoSet);
};

/* Create unique set of item sets
 * */
lrGeneratorMixin.canonicalCollection = function canonicalCollection () {
    var item1 = new this.Item(this.productions[0], 0, new Set(this.EOF));
    var firstState = this.closureOperation(new this.ItemSet(item1)),
        states = new Set(firstState),
        marked = 0,
        self = this,
        itemSet;

    states.has = {};
    states.has[firstState] = 0;

    while (marked !== states.size()) {
        itemSet = states.item(marked); marked++;
        itemSet.forEach(function CC_itemSet_forEach (item) {
            if (item.markedSymbol && item.markedSymbol !== self.EOF)
                self.canonicalCollectionInsert(item.markedSymbol, itemSet, states, marked-1);
        });
    }

    return states;
};

// Pushes a unique state into the que. Some parsing algorithms may perform additional operations
lrGeneratorMixin.canonicalCollectionInsert = function canonicalCollectionInsert (symbol, itemSet, states, stateNum) {
    var g = this.gotoOperation(itemSet, symbol);
    if (!g.predecessors)
        g.predecessors = {};
    // add g to que if not empty or duplicate
    if (!g.isEmpty()) {
        var gv = g.valueOf(),
            i = states.has[gv];
        if (i === -1 || typeof i === 'undefined') {
            states.has[gv] = states.size();
            itemSet.edges[symbol] = states.size(); // store goto transition for table
            states.push(g);
            g.predecessors[symbol] = [stateNum];
        } else {
            itemSet.edges[symbol] = i; // store goto transition for table
            states.item(i).predecessors[symbol].push(stateNum);
        }
    }
};

var NONASSOC = 0;
lrGeneratorMixin.parseTable = function parseTable (itemSets) {
    var states = [],
        nonterminals = this.nonterminals,
        operators = this.operators,
        self = this,
        s = 1, // shift
        r = 2, // reduce
        a = 3; // accept

    // for each item set
    itemSets.forEach(function (itemSet, k) {
        var state = states[k] = {};
        var action, stackSymbol;

        // set shift and goto actions
        for (stackSymbol in itemSet.edges) {
            itemSet.forEach(function (item, j) {
                // find shift and goto actions
                if (item.markedSymbol == stackSymbol) {
                    var gotoState = itemSet.edges[stackSymbol];
                    if (nonterminals[stackSymbol]) {
                        // store state to go to after a reduce
                        //self.trace(k, stackSymbol, 'g'+gotoState);
                        state[self.symbols_[stackSymbol]] = gotoState;
                    } else {
                        //self.trace(k, stackSymbol, 's'+gotoState);
                        state[self.symbols_[stackSymbol]] = [s,gotoState];
                    }
                }
            });
        }

        // set accept action
        itemSet.forEach(function (item, j) {
            if (item.markedSymbol == self.EOF) {
                // accept
                state[self.symbols_[self.EOF]] = [a];
                //self.trace(k, self.EOF, state[self.EOF]);
            }
        });

        var allterms = self.lookAheads ? false : self.terminals;

        // set reductions and resolve potential conflicts
        itemSet.reductions.forEach(function (item, j) {
            // if parser uses lookahead, only enumerate those terminals
            var terminals = allterms || self.lookAheads(itemSet, item);

            terminals.forEach(function (stackSymbol) {
                action = state[self.symbols_[stackSymbol]];
                var op = operators[stackSymbol];

                // Reading a terminal and current position is at the end of a production, try to reduce
                if (action || action && action.length) {
                    var sol = resolveConflict(item.production, op, [r,item.production.id], action[0] instanceof Array ? action[0] : action);
                    self.resolutions.push([k,stackSymbol,sol]);
                    if (sol.bydefault) {
                        self.conflicts++;
                        if (!self.DEBUG) {
                            self.warn('Conflict in grammar (state:',k, ', token:',stackSymbol, ")\n  ", printAction(sol.r, self), "\n  ", printAction(sol.s, self));
                        }
                        if (self.options.noDefaultResolve) {
                            if (!(action[0] instanceof Array))
                                action = [action];
                            action.push(sol.r);
                        }
                    } else {
                        action = sol.action;
                    }
                } else {
                    action = [r,item.production.id];
                }
                if (action && action.length) {
                    state[self.symbols_[stackSymbol]] = action;
                } else if (action === NONASSOC) {
                    state[self.symbols_[stackSymbol]] = undefined;
                }
            });
        });

    });

    return states;
};

// resolves shift-reduce and reduce-reduce conflicts
function resolveConflict (production, op, reduce, shift) {
    var sln = {production: production, operator: op, r: reduce, s: shift},
        s = 1, // shift
        r = 2, // reduce
        a = 3; // accept

    if (shift[0] === r) {
        sln.msg = "Resolve R/R conflict (use first production declared in grammar.)";
        sln.action = shift[1] < reduce[1] ? shift : reduce;
        sln.bydefault = true;
        return sln;
    }

    if (production.precedence === 0 || !op) {
        sln.msg = "Resolve S/R conflict (shift by default.)";
        sln.bydefault = true;
        sln.action = shift;
    } else if (production.precedence < op.precedence ) {
        sln.msg = "Resolve S/R conflict (shift for higher precedent operator.)";
        sln.action = shift;
    } else if (production.precedence === op.precedence) {
        if (op.assoc === "right" ) {
            sln.msg = "Resolve S/R conflict (shift for right associative operator.)";
            sln.action = shift;
        } else if (op.assoc === "left" ) {
            sln.msg = "Resolve S/R conflict (reduce for left associative operator.)";
            sln.action = reduce;
        } else if (op.assoc === "nonassoc" ) {
            sln.msg = "Resolve S/R conflict (no action for non-associative operator.)";
            sln.action = NONASSOC;
        }
    } else {
        sln.msg = "Resolve conflict (reduce for higher precedent production.)";
        sln.action = reduce;
    }

    return sln;
}

lrGeneratorMixin.generate = function parser_generate (opt) {
    opt = typal.mix.call({}, this.options, opt);
    var code = "";
    switch (opt.moduleType) {
        case "js":
            code = this.generateModule(opt);
        break;
        case "commonjs":
        default:
            code = this.generateCommonJSModule(opt);
    }

    return code;
};

lrGeneratorMixin.generateCommonJSModule = function generateCommonJSModule (opt) {
    opt = typal.mix.call({}, this.options, opt);
    var moduleName = opt.moduleName || "parser";
    var out = this.generateModule(opt);
    out += "\nif (typeof require !== 'undefined') {";
    out += "\nexports.parser = "+moduleName+";";
    out += "\nexports.parse = function () { return "+moduleName+".parse.apply("+moduleName+", arguments); }";
    out += "\nexports.main = "+ String(opt.moduleMain || commonjsMain);
    out += "\nif (require.main === module) {\n\texports.main(require(\"system\").args);\n}";
    out += "\n}";

    return out;
};

lrGeneratorMixin.generateModule = function generateModule (opt) {
    opt = typal.mix.call({}, this.options, opt);
    var moduleName = opt.moduleName || "parser";
    var out = "/* Jison generated parser */\n";
    out += (moduleName.match(/\./) ? moduleName : "var "+moduleName)+" = (function(){";
    out += "\nvar parser = "+this.generateModule_();
    if (this.lexer && this.lexer.generateModule) {
        out += this.lexer.generateModule();
        out += "\nparser.lexer = lexer;";
    }
    out += "\nreturn parser;\n})();";

    return out;
};

lrGeneratorMixin.generateModule_ = function generateModule_ () {
    var out = "{";
    out += [
        "trace: " + String(this.trace || parser.trace),
        "yy: {}",
        "symbols_: " + JSON.stringify(this.symbols_),
        "terminals_: " + JSON.stringify(this.terminals_),
        "productions_: " + JSON.stringify(this.productions_),
        "performAction: " + String(this.performAction),
        "table: " + JSON.stringify(this.table),
        "parseError: " + String(this.parseError || (this.hasErrorRecovery ? traceParseError : parser.parseError)),
        "parse: " + String(parser.parse)
        ].join(",\n");
    out += "};";

    return out;
};

// default main method for generated commonjs modules
function commonjsMain (args) {
    var cwd = require("file").path(require("file").cwd());
    if (!args[1])
        throw new Error('Usage: '+args[0]+' FILE');
    var source = cwd.join(args[1]).read({charset: "utf-8"});
    exports.parser.parse(source);
}

// debug mixin for LR parser generators

function printAction (a, gen) {
    var s = a[0] == 1 ? 'shift '+gen.symbols[a[1]] :
        a[0] == 2 ? 'reduce by '+gen.productions[a[1]] :
                    'accept' ;

    return s;
}

var lrGeneratorDebug = {
    beforeparseTable: function () {
        this.trace("Building parse table.");
    },
    afterparseTable: function () {
        var self = this;
        if (this.conflicts > 0) {
            this.resolutions.forEach(function (r, i) {
                if (r[2].bydefault) {
                    self.warn('Conflict at state:',r[0], ', Token:',r[1], "\n  ", printAction(r[2].r, self), "\n  ", printAction(r[2].s, self));
                }
            });
            this.trace("\n"+this.conflicts+" Conflict(s) found in grammar.");
        }
        this.trace("Done.");
    },
    aftercanonicalCollection: function (states) {
        var trace = this.trace;
        trace("\nItem sets\n------");

        states.forEach(function (state, i) {
            trace("\nitem set",i,"\n"+state.join("\n"), '\ntransitions -> ', JSON.stringify(state.edges));
        });
    }
};

var parser = typal.beget();

lrGeneratorMixin.createParser = function createParser () {
    var p = parser.beget();
    p.yy = {};

    p.init({
        table: this.table,
        productions_: this.productions_,
        symbols_: this.symbols_,
        terminals_: this.terminals_,
        performAction: this.performAction
    });

    // don't throw if grammar recovers from errors
    if (this.hasErrorRecovery) {
        p.parseError = traceParseError;
    }

    // for debugging
    p.productions = this.productions;

    // backwards compatability
    p.generate = this.generate;
    p.lexer = this.lexer;
    p.generateModule = this.generateModule;
    p.generateCommonJSModule = this.generateCommonJSModule;
    p.generateModule_ = this.generateModule_;

    return p;
};

parser.trace = generator.trace;
parser.warn = generator.warn;
parser.error = generator.error;

function traceParseError (err, hash) {
    this.trace(err);
}

parser.parseError = lrGeneratorMixin.parseError = function parseError (str, hash) {
    throw new Error(str);
};

parser.parse = function parse (input) {
    var self = this,
        stack = [0],
        vstack = [null], // semantic value stack
        table = this.table,
        yytext = '',
        yylineno = 0,
        yyleng = 0,
        shifts = 0,
        reductions = 0,
        recovering = 0,
        TERROR = 2,
        EOF = 1;

    this.lexer.setInput(input);
    this.lexer.yy = this.yy;
    this.yy.lexer = this.lexer;

    var parseError = this.yy.parseError = typeof this.yy.parseError == 'function' ? this.yy.parseError : this.parseError;

    function popStack (n) {
        stack.length = stack.length - 2*n;
        vstack.length = vstack.length - n;
    }

    function checkRecover (st) {
        for (var p in table[st]) if (p == TERROR) {
            //print('RECOVER!!');
            return true;
        }
        return false;
    }

    function lex() {
        var token;
        token = self.lexer.lex() || 1; // $end = 1
        // if token isn't its numeric value, convert
        if (typeof token !== 'number') {
            token = self.symbols_[token];
        }
        return token;
    };

    var symbol, preErrorSymbol, state, action, a, r, yyval={},p,len,newState, expected, recovered = false;
    symbol = lex();
    while (true) {
        // set first input
        state = stack[stack.length-1];
        // read action for current state and first input
        action = table[state] && table[state][symbol];

        // handle parse error
        if (typeof action === 'undefined' || !action.length || !action[0]) {

            if (!recovering) {
                // Report error
                expected = [];
                for (p in table[state]) if (this.terminals_[p] && p > 2) {
                    expected.push("'"+this.terminals_[p]+"'");
                }
                if (this.lexer.showPosition) {
                    parseError.call(this, 'Parse error on line '+(yylineno+1)+":\n"+this.lexer.showPosition()+'\nExpecting '+expected.join(', '),
                        {text: this.lexer.match, token: this.terminals_[symbol] || symbol, line: this.lexer.yylineno, expected: expected});
                } else {
                    parseError.call(this, 'Parse error on line '+(yylineno+1)+": Unexpected '"+this.terminals_[symbol]+"'",
                        {text: this.lexer.match, token: this.terminals_[symbol] || symbol, line: this.lexer.yylineno, expected: expected});
                }
            }

            // just recovered from another error
            if (recovering == 3) {
                if (symbol == EOF) {
                    throw 'Parsing halted.'
                }

                // discard current lookahead and grab another
                yyleng = this.lexer.yyleng;
                yytext = this.lexer.yytext;
                yylineno = this.lexer.yylineno;
                symbol = lex();
            }

            // try to recover from error
            while (1) {
                // check for error recovery rule in this state
                if (checkRecover(state)) {
                    break;
                }
                if (state == 0) {
                    throw 'Parsing halted.'
                }
                popStack(1);
                state = stack[stack.length-1];
            }

            preErrorSymbol = symbol; // save the lookahead token
            symbol = TERROR;         // insert generic error symbol as new lookahead
            state = stack[stack.length-1];
            action = table[state] && table[state][TERROR];
            recovering = 3; // allow 3 real symbols to be shifted before reporting a new error
        }

        // this shouldn't happen, unless resolve defaults are off
        if (action[0] instanceof Array && action.length > 1) {
            throw new Error('Parse Error: multiple actions possible at state: '+state+', token: '+symbol);
        }

        a = action;

        switch (a[0]) {

            case 1: // shift
                shifts++;

                stack.push(symbol);
                vstack.push(this.lexer.yytext); // semantic values or junk only, no terminals
                stack.push(a[1]); // push state
                if (!preErrorSymbol) { // normal execution/no error
                    yyleng = this.lexer.yyleng;
                    yytext = this.lexer.yytext;
                    yylineno = this.lexer.yylineno;
                    symbol = lex();
                    if (recovering > 0)
                        recovering--;
                } else { // error just occurred, resume old lookahead f/ before error
                    symbol = preErrorSymbol;
                    preErrorSymbol = null;
                }
                break;

            case 2: // reduce
                reductions++;

                len = this.productions_[a[1]][1];

                // perform semantic action
                yyval.$ = vstack[vstack.length-len]; // default to $$ = $1
                r = this.performAction.call(yyval, yytext, yyleng, yylineno, this.yy, a[1], vstack);

                if (typeof r !== 'undefined') {
                    return r;
                }

                // pop off stack
                if (len) {
                    stack = stack.slice(0,-1*len*2);
                    vstack = vstack.slice(0, -1*len);
                }

                stack.push(this.productions_[a[1]][0]);    // push nonterminal (reduce)
                vstack.push(yyval.$);
                // goto new state = table[STATE][NONTERMINAL]
                newState = table[stack[stack.length-2]][stack[stack.length-1]];
                stack.push(newState);
                break;

            case 3: // accept

                this.reductionCount = reductions;
                this.shiftCount = shifts;
                return true;
        }

    }

    return true;
};

parser.init = function parser_init (dict) {
    this.table = dict.table;
    this.performAction = dict.performAction;
    this.productions_ = dict.productions_;
    this.symbols_ = dict.symbols_;
    this.terminals_ = dict.terminals_;
};

/*
 * LR(0) Parser
 * */

var lr0 = generator.beget(lookaheadMixin, lrGeneratorMixin, {
    type: "LR(0)",
    afterconstructor: function lr0_afterconstructor () {
        this.buildTable();
    }
});

var LR0Generator = exports.LR0Generator = lr0.construct();

/*
 * Simple LALR(1)
 * */

var lalr = generator.beget(lookaheadMixin, lrGeneratorMixin, {
    type: "LALR(1)",

    afterconstructor: function (grammar, options) {
        if (this.DEBUG) this.mix(lrGeneratorDebug, lalrGeneratorDebug); // mixin debug methods

        options = options || {};
        this.states = this.canonicalCollection();
        this.terms_ = {};

        var newg = this.newg = typal.beget(lookaheadMixin,{
            oldg: this,
            trace: this.trace,
            nterms_: {},
            DEBUG: false,
            go_: function (r, B) {
                r = r.split(":")[0]; // grab state #
                B = B.map(function (b) { return b.slice(b.indexOf(":")+1)});
                return this.oldg.go(r, B);
            }
        });
        newg.nonterminals = {};
        newg.productions = [];

        this.inadequateStates = [];

        // if true, only lookaheads in inadequate states are computed (faster, larger table)
        // if false, lookaheads for all reductions will be computed (slower, smaller table)
        this.onDemandLookahead = options.onDemandLookahead || false;

        this.buildNewGrammar();
        newg.computeLookaheads();
        this.unionLookaheads();

        this.table = this.parseTable(this.states);
    },

    lookAheads: function LALR_lookaheads (state, item) {
        return (!!this.onDemandLookahead && !state.inadequate) ? this.terminals : item.follows;
    },
    go: function LALR_go (p, w) {
        var q = parseInt(p);
        for (var i=0;i<w.length;i++) {
            q = this.states.item(q).edges[w[i]] || q;
        }
        return q;
    },
    goPath: function LALR_goPath (p, w) {
        var q = parseInt(p),t,
            path = [];
        for (var i=0;i<w.length;i++) {
            t = w[i] ? q+":"+w[i] : '';
            if (t) this.newg.nterms_[t] = q;
            path.push(t);
            q = this.states.item(q).edges[w[i]] || q;
            this.terms_[t] = w[i];
        }
        return {path: path, endState: q};
    },
    // every disjoint reduction of a nonterminal becomes a produciton in G'
    buildNewGrammar: function LALR_buildNewGrammar () {
        var self = this,
            newg = this.newg;

        this.states.forEach(function (state, i) {
            state.forEach(function (item) {
                if (item.dotPosition === 0) {
                    // new symbols are a combination of state and transition symbol
                    var symbol = i+":"+item.production.symbol;
                    self.terms_[symbol] = item.production.symbol;
                    newg.nterms_[symbol] = i;
                    if (!newg.nonterminals[symbol])
                        newg.nonterminals[symbol] = new Nonterminal(symbol);
                    var pathInfo = self.goPath(i, item.production.handle);
                    var p = new Production(symbol, pathInfo.path, newg.productions.length);
                    newg.productions.push(p);
                    newg.nonterminals[symbol].productions.push(p);

                    // store the transition that get's 'backed up to' after reduction on path
                    var handle = item.production.handle.join(' ');
                    var goes = self.states.item(pathInfo.endState).goes;
                    if (!goes[handle])
                        goes[handle] = [];
                    goes[handle].push(symbol);

                    //self.trace('new production:',p);
                }
            });
            if (state.inadequate)
                self.inadequateStates.push(i);
        });
    },
    unionLookaheads: function LALR_unionLookaheads () {
        var self = this,
            newg = this.newg,
            states = !!this.onDemandLookahead ? this.inadequateStates : this.states;

        states.forEach(function union_states_forEach (i) {
            var state = typeof i === 'number' ? self.states.item(i) : i,
                follows = [];
            if (state.reductions.length)
            state.reductions.forEach(function union_reduction_forEach (item) {
                var follows = {};
                for (var k=0;k<item.follows.length;k++) {
                    follows[item.follows[k]] = true;
                }
                state.goes[item.production.handle.join(' ')].forEach(function reduction_goes_forEach (symbol) {
                    newg.nonterminals[symbol].follows.forEach(function goes_follows_forEach (symbol) {
                        var terminal = self.terms_[symbol];
                        if (!follows[terminal]) {
                            follows[terminal]=true;
                            item.follows.push(terminal);
                        }
                    });
                });
                //self.trace('unioned item', item);
            });
        });
    }
});

var LALRGenerator = exports.LALRGenerator = lalr.construct();

// LALR generator debug mixin

var lalrGeneratorDebug = {
    trace: function trace () {
        Jison.print.apply(null, arguments);
    },
    beforebuildNewGrammar: function () {
        this.trace(this.states.size()+" states.");
        this.trace("Building lookahead grammar.");
    },
    beforeunionLookaheads: function () {
        this.trace("Computing lookaheads.");
    }
};

/*
 * Lookahead parser definitions
 *
 * Define base type
 * */
var lrLookaheadGenerator = generator.beget(lookaheadMixin, lrGeneratorMixin, {
    afterconstructor: function lr_aftercontructor () {
        this.computeLookaheads();
        this.buildTable();
    }
});

/*
 * SLR Parser
 * */
var SLRGenerator = exports.SLRGenerator = lrLookaheadGenerator.construct({
    type: "SLR(1)",

    lookAheads: function SLR_lookAhead (state, item) {
        return this.nonterminals[item.production.symbol].follows;
    }
});

/*
 * LR(1) Parser
 * */
var lr1 = lrLookaheadGenerator.beget({
    type: "Canonical LR(1)",

    lookAheads: function LR_lookAheads (state, item) {
        return item.follows;
    },
    Item: lrGeneratorMixin.Item.prototype.construct({
        afterconstructor: function () {
            this.id = this.production.id+'a'+this.dotPosition+'a'+this.follows.sort().join(',');
        },
        eq: function (e) {
            return e.id === this.id;
        }
    }),

    closureOperation: function LR_ClosureOperation (itemSet /*, closureSet*/) {
        var closureSet = new this.ItemSet();
        var self = this;

        var set = itemSet,
            itemQueue, syms = {};

        do {
        itemQueue = new Set();
        closureSet.concat(set);
        set.forEach(function (item) {
            var symbol = item.markedSymbol;
            var b;

            // if token is a nonterminal, recursively add closures
            if (symbol && self.nonterminals[symbol]) {
                b = self.first(item.remainingHandle());
                if (b.length === 0) b = item.follows;
                self.nonterminals[symbol].productions.forEach(function (production) {
                    var newItem = new self.Item(production, 0, b);
                    if(!closureSet.contains(newItem) && !itemQueue.contains(newItem)) {
                        itemQueue.push(newItem);
                    }
                });
            } else if (!symbol) {
                // reduction
                closureSet.reductions.push(item);
            }
        });

        set = itemQueue;
        } while (!itemQueue.isEmpty());

        return closureSet;
    }
});

var LR1Generator = exports.LR1Generator = lr1.construct();

/*
 * LL Parser
 * */
var ll = generator.beget(lookaheadMixin, {
    type: "LL(1)",

    afterconstructor: function ll_aftercontructor () {
        this.computeLookaheads();
        this.table = this.parseTable(this.productions);
    },
    parseTable: function llParseTable (productions) {
        var table = {},
            self = this;
        productions.forEach(function (production, i) {
            var row = table[production.symbol] || {};
            var tokens = production.first;
            if (self.nullable(production.handle)) {
                Set.union(tokens, self.nonterminals[production.symbol].follows);
            }
            tokens.forEach(function (token) {
                if (row[token]) {
                    row[token].push(i);
                    self.conflicts++;
                } else {
                    row[token] = [i];
                }
            });
            table[production.symbol] = row;
        });

        return table;
    }
});

var LLGenerator = exports.LLGenerator = ll.construct();

Jison.Generator = function Jison_Generator (g, options) {
    var opt = typal.mix.call({}, g.options, options);
    switch (opt.type) {
        case 'lr0':
            return new LR0Generator(g, opt);
        case 'slr':
            return new SLRGenerator(g, opt);
        case 'lr':
            return new LR1Generator(g, opt);
        case 'll':
            return new LLGenerator(g, opt);
        case 'lalr':
        default:
            return new LALRGenerator(g, opt);
    }
}

return function Parser (g, options) {
        var opt = typal.mix.call({}, g.options, options);
        var gen;
        switch (opt.type) {
            case 'lr0':
                gen = new LR0Generator(g, opt);
            case 'slr':
                gen = new SLRGenerator(g, opt);
            case 'lr':
                gen = new LR1Generator(g, opt);
            case 'll':
                gen = new LLGenerator(g, opt);
            case 'lalr':
            default:
                gen = new LALRGenerator(g, opt);
        }
        return gen.createParser();
    }

})();

exports.main = function main (args) {
    //var parser = new require("args").Parser();
    var fs = require("file");
        gfile = fs.path(fs.cwd()).join(args[1]);

    // try to parse as JSON, else use BNF parser
    if (gfile.extension() === '.json') {
        var grammar = JSON.parse(gfile.read({charset: "utf-8"}));
    } else if (gfile.extension() === '.jison') {
        var grammar = require("jison/bnf").parse(gfile.read({charset: "utf-8"}));
    }

    var opt = grammar.options || {};

    // lexer file
    if (args[2]) {
        var lfile = fs.path(fs.cwd()).join(args[2]);

        // try to parse as JSON, else use BNF parser
        if (lfile.extension() === '.json') {
            grammar.lex = JSON.parse(lfile.read({charset: "utf-8"}));
        } else if (lfile.extension() === '.jisonlex') {
            grammar.lex = require("jison/jisonlex").parse(lfile.read({charset: "utf-8"}));
        }
    }

    if (!opt.moduleName)
        opt.moduleName = gfile.basename().replace(new RegExp(gfile.extension()+"$"), "");
    if (!opt.moduleType)
        opt.moduleType = "commonjs";

    var generator = new Jison.Generator(grammar, opt);
        fname = fs.path(fs.cwd()).join(opt.moduleName + ".js"),
        source = generator.generate(opt),
        stream = fname.open("w");

    stream.print(source);
    stream.close();
};


text:
package mondo

import (
	"encoding/json"
	"io/ioutil"
	"net/http"
)

type httpclient interface {
	Do(*http.Request) (*http.Response, error)
}

type auth interface {
	Get(invalidate bool, client *Client) (string, error)
}

// Client wraps Mondo-specific error-handling and authentication around an HTTP
// Client.
type Client struct {
	HTTPClient httpclient
	Auth       auth
}

// Do performs a request and returns the raw HTTP response. Any authorization
// headers on the request are overridden by what the Client's Auth provides.
func (c *Client) Do(req *http.Request) (*http.Response, error) {
	_, authedReq := req.Header[http.CanonicalHeaderKey("Authorization")]
	if !authedReq || c.Auth == nil {
		return c.do(req)
	}
	return c.doAuth(req)
}

// DoInto performs a request and stores the result into the given object. Any
// authorization headers on the request are overridden by what the Client's
// Auth provides.
func (c *Client) DoInto(req *http.Request, target interface{}) error {
	resp, err := c.Do(req)
	if err != nil {
		return err
	}

	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return WrapError(err, "Failed to read response body", req, resp)
	}

	err = json.Unmarshal(body, target)
	if err != nil {
		return WrapError(err, "Failed to parse response body", req, resp)
	}

	return nil
}

func (c *Client) do(req *http.Request) (*http.Response, error) {
	resp, err := c.HTTPClient.Do(req)
	if err != nil {
		return resp, WrapError(err, "HTTP request failed", req, resp)
	}

	if resp.StatusCode != 200 {
		respErr := DecodeError(req, resp)
		if _, ok := respErr.(*ResponseError); ok {
			return resp, respErr
		}
		return resp, WrapError(respErr, "Non-200 response returned and an error occured during parsing", req, resp)
	}

	return resp, nil
}

func (c *Client) doAuth(req *http.Request) (*http.Response, error) {
	var resp *http.Response
	var _err error

	for attempt := 0; attempt < 2; attempt++ {
		// Get the token. Get a fresh token in subsequent attempts.
		invalidate := attempt > 0
		token, err := c.Auth.Get(invalidate, c)
		if err != nil {
			return nil, WrapError(err, "Failed to get authentication details", req, nil)
		}

		req.Header.Set("Authorization", token)
		resp, err = c.do(req)
		if err != nil {
			if mondoErr, ok := err.(*ResponseError); ok && mondoErr.InvalidToken {
				_err = mondoErr
				continue
			}
			return resp, err
		}

		break
	}

	return resp, _err
}


text:
/*
 * $Id: CopyChainVisitor.h 5943 2013-03-03 18:39:03Z johnnyw $
 *
 *
 * Distributed under the OpenDDS License.
 * See: http://www.opendds.org/license.html
 */

#ifndef OPENDDS_DCPS_COPYCHAINVISTOR_H
#define OPENDDS_DCPS_COPYCHAINVISTOR_H

#include "dds/DCPS/dcps_export.h"
#include "BasicQueue_T.h"
#include "TransportRetainedElement.h"

ACE_BEGIN_VERSIONED_NAMESPACE_DECL
class ACE_Message_Block;
ACE_END_VERSIONED_NAMESPACE_DECL

namespace OpenDDS {
namespace DCPS {

class TransportQueueElement;

class OpenDDS_Dcps_Export CopyChainVisitor : public BasicQueueVisitor<TransportQueueElement> {
public:

  CopyChainVisitor(
    BasicQueue<TransportQueueElement>& target,
    TransportRetainedElementAllocator* allocator,
    MessageBlockAllocator*             mb_allocator,
    DataBlockAllocator*                db_allocator
  );

  virtual ~CopyChainVisitor();

  virtual int visit_element(TransportQueueElement* element);

  /// Access the status.
  int status() const;

private:
  /// Target queue to fill with copied elements.
  BasicQueue<TransportQueueElement>& target_;

  /// Allocator to create copied elements.
  TransportRetainedElementAllocator* allocator_;
  MessageBlockAllocator* mb_allocator_;
  DataBlockAllocator* db_allocator_;

  /// Status of visitation.
  int status_;
};

} // namespace DCPS
} // namespace OpenDDS

#if defined (__ACE_INLINE__)
#include "CopyChainVisitor.inl"
#endif /* __ACE_INLINE__ */

#endif  /* OPENDDS_DCPS_COPYCHAINVISTOR_H */


text:
OpaqueBrowserFrameViewLinux::OpaqueBrowserFrameViewLinux(
    OpaqueBrowserFrameView* view,
    OpaqueBrowserFrameViewLayout* layout)
    : view_(view),
      layout_(layout) {
  views::LinuxUI* ui = views::LinuxUI::instance();
  if (ui)
    ui->AddWindowButtonOrderObserver(this);
}

OpaqueBrowserFrameViewLinux::~OpaqueBrowserFrameViewLinux() {
  views::LinuxUI* ui = views::LinuxUI::instance();
  if (ui)
    ui->RemoveWindowButtonOrderObserver(this);
}

///////////////////////////////////////////////////////////////////////////////
// OpaqueBrowserFrameViewLinux,
//     views::WindowButtonOrderObserver implementation:

void OpaqueBrowserFrameViewLinux::OnWindowButtonOrderingChange(
    const std::vector<views::FrameButton>& leading_buttons,
    const std::vector<views::FrameButton>& trailing_buttons) {
  layout_->SetButtonOrdering(leading_buttons, trailing_buttons);

  // We can receive OnWindowButtonOrderingChange events before we've been added
  // to a Widget. We need a Widget because layout crashes due to dependencies
  // on a ui::ThemeProvider().
  if (view_->GetWidget()) {
    view_->Layout();
    view_->SchedulePaint();
  }
}

///////////////////////////////////////////////////////////////////////////////
// OpaqueBrowserFrameViewObserver:

// static
OpaqueBrowserFrameViewPlatformSpecific*
OpaqueBrowserFrameViewPlatformSpecific::Create(
      OpaqueBrowserFrameView* view,
      OpaqueBrowserFrameViewLayout* layout) {
  return new OpaqueBrowserFrameViewLinux(view, layout);
}


text:
#ifndef RANDOMGENERATOR_H_
#define RANDOMGENERATOR_H_

#include <random>
#include <armadillo>

namespace ReLe
{
/*!
 * This class implements function to generate Random Number Generators.
 */
class RngGenerators
{
public:
    /*!
     * Constructor.
     * This function initializes the Random Number Generator of std and armadillo
     * libraries and the Random Number Generator of the class attribute with the provided seed.
     * \param seed the seed to be set
     */
    RngGenerators(unsigned int seed) : gen(seed)
    {
        srand(seed);
        arma::arma_rng::set_seed(seed);
    }

    /*!
     * Modifies the Random Number Generator of std and armadillo
     * libraries and the Random Number Generator of the class attribute with the provided seed.
     * \param seed the seed to be set
     */
    void seed(unsigned int seed)
    {
        srand(seed);
        arma::arma_rng::set_seed(seed);
        gen.seed(seed);
    }

    //! Random Number Generator variable based on Mersenne Twister Algorithm.
    std::mt19937 gen;
};

/*!
 * This class has some useful function to sample random numbers from
 * different probability distributions.
 */
class RandomGenerator
{
public:
    /*!
     * Generate a random unsigned integer number of 32 bits.
     * \return the random unsigned integer number
     */
    inline static uint32_t randu32()
    {
        return gen.gen();
    }

    /*!
     * Sample values from a normal distribution with mean = 0 and std = 1.
     * \return the random number sampled from the normal distribution
     */
    inline static double sampleNormal()
    {
        std::normal_distribution<> dist;
        return dist(gen.gen);
    }

    /*!
     * Sample values from a normal distribution with provided mean and standard deviation.
     * \param m the mean of the normal distribution
     * \param sigma the standard deviation of the normal distribution
     * \return the random number sampled from the normal distribution
     */
    inline static double sampleNormal(double m, double sigma)
    {
        std::normal_distribution<> dist(m, sigma);
        return dist(gen.gen);
    }

    inline static double sampleLogNormal()
    {
        std::lognormal_distribution<> dist;
        return dist(gen.gen);
    }

    inline static double sampleLogNormal(double m, double sigma)
    {
        std::lognormal_distribution<> dist(m, sigma);
        return dist(gen.gen);
    }

    /*!
     * Sample values from a uniform distribution with provided lower and higher values
     * where the higher one is excluded from the range.
     * \param lo the lower value of the uniform distribution
     * \param hi the upper value of the uniform distribution
     * \return the random number sampled from the uniform distribution
     */
    inline static double sampleUniform(const double lo, const double hi)
    {
        std::uniform_real_distribution<> dist(lo, hi);
        return dist(gen.gen);
    }

    /*!
     * Sample values from a uniform distribution with provided lower and higher values
     * where the lower one is excluded from the range.
     * \param lo the lower value of the uniform distribution
     * \param hi the upper value of the uniform distribution
     * \return the random number sampled from the uniform distribution
     */
    inline static double sampleUniformHigh(const double lo, const double hi)
    {
        std::uniform_real_distribution<> dist(-hi, -lo);
        return -dist(gen.gen);
    }

    /*!
     * Sample integer values from a uniform distribution with provided lower and higher values
     * where the lower one is excluded from the range.
     * \param lo the lower value of the uniform distribution
     * \param hi the upper value of the uniform distribution
     * \return the integer random number sampled from the uniform distribution
     */
    inline static std::size_t sampleUniformInt(const int lo, const int hi)
    {
        std::uniform_int_distribution<> dist(lo, hi);
        return dist(gen.gen);
    }

    /*!
     * Sample a random integer from 0 to n where n is the number of elements
     * of the given probability vector which indicates the probability of each
     * number to be sampled.
     * \param prob vector of probabilities
     * \return the sampled number
     */
    inline static std::size_t sampleDiscrete(std::vector<double>& prob)
    {
        std::discrete_distribution<std::size_t> dist(prob.begin(),
                prob.end());
        return dist(gen.gen);
    }

    /*!
     * Template function to sample a random integer from 0 to n where n is the number of elements
     * of the given probability vector (of arbitrary type) which indicates
     * the probability of each number to be sampled.
     * \param begin the first element of the probabilities vector
     * \param end the last element of the probabilities vector
     * \return the sampled number
     */
    template<class Iterator>
    inline static std::size_t sampleDiscrete(Iterator begin, Iterator end)
    {
        std::discrete_distribution<std::size_t> dist(begin, end);
        return dist(gen.gen);
    }

    /*!
     * Sample the outcome of an event (false or true) from a uniform distribution
     * with given probability.
     * \param prob the probability of the event
     * \return a bool value representing the happening of an event
     */
    inline static bool sampleEvent(double prob)
    {
        std::uniform_real_distribution<> dist(0, 1);
        return dist(gen.gen) < prob;
    }

    /*!
     * Set the seed of the Random Number Generator with a given seed.
     * \param seed the seed to be set
     */
    inline static void seed(unsigned int seed)
    {
        gen.seed(seed);
    }

private:
    //random generators
    static std::random_device rd;
    static RngGenerators gen;

};

}

#endif /* RANDOMGENERATOR_H_ */


text:
using namespace std;

class SSDPDBDevice{
public:
	SSDPDBDevice();
	~SSDPDBDevice();
	u8 isdevice;
	u8 isroot;
	u8 isservice;
	string uuid;
	string urn;
	string usn;
	ssdpuuid parsedusn;
	string type;
	string version;
	string host;
	string location;

	u32 ip;
	u16 port;

	u32 cachecontrol;
	u32 lastupdate;

private:
	SSDPDBDevice(const SSDPDBDevice &src);
	SSDPDBDevice& operator= (const SSDPDBDevice &src);
};

#endif //_SSDPDBDEVICE_H


text:
package com.luoxiang.weibo.utils;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;

import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;

import static android.content.ContentValues.TAG;

/**
 * projectName: 	    WeiBo
 * packageName:	        com.luoxiang.weibo.utils
 * className:	        FileUtil
 * author:	            Luoxiang
 * time:	            2016/12/31	10:13
 * desc:	            删除文件
 *
 * svnVersion:	        $Rev
 * upDateAuthor:	    Vincent
 * upDate:	            2016/12/31
 * upDateDesc:	        TODO
 */

public class FileUtil {
    /**
     * 删除整个目录
     * @param path 路径
     */
    public static void deleteAllFilesOfDir(File path) {
        if (!path.exists())
            return;
        if (path.isFile()) {
            path.delete();
            return;
        }
        File[] files = path.listFiles();
        for (int i = 0; i < files.length; i++) {
            deleteAllFilesOfDir(files[i]);
        }
        //如果不删除文件夹目录 注释即可
        path.delete();
    }

    /**
     * 命令行模式删除 需要root
     * @param path 路径
     */
    public static void delDir(String path){
        String cmd = "rm -r " + path;
        Log.d(TAG, cmd);
        execCommand(cmd);
    }

    static void execCommand(String command){
        DataOutputStream outputStream = null;
        try {
            Process su = Runtime.getRuntime()
                                .exec("su");
            outputStream = new DataOutputStream(su.getOutputStream());

        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            outputStream.writeBytes(command + "\n");
            outputStream.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 挂载文件
     * @param context 上下文
     * @param out 文件
     *            需要权限<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
     */
    public static void mountFile(Context context , File out){
        context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(out)));
    }
}


text:
using content::BrowserThread;

namespace net {
class URLRequestContextGetter;
}

namespace {
const int kSessionIDForTests = 0;
}

// Mock class used to test the extension speech input API.
class SpeechInputExtensionApiTest : public ExtensionApiTest,
                                    public SpeechInputExtensionInterface {
 public:
  SpeechInputExtensionApiTest();
  virtual ~SpeechInputExtensionApiTest();

  void SetRecordingDevicesAvailable(bool available) {
    recording_devices_available_ = available;
  }

  void SetRecognitionError(content::SpeechRecognitionErrorCode error) {
    next_error_ = error;
  }

  void SetRecognitionResult(const content::SpeechRecognitionResult& result) {
    next_result_ = result;
  }

  void SetRecognitionDelay(int result_delay_ms) {
    result_delay_ms_ = result_delay_ms;
  }

  // Used as delay when the corresponding call should not be dispatched.
  static const int kDontDispatchCall = -1;

  // InProcessBrowserTest methods.
  virtual void SetUpOnMainThread() OVERRIDE {
    manager_ = SpeechInputExtensionManager::GetForProfile(browser()->profile());
  }

  // ExtensionApiTest methods.
  virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
    ExtensionApiTest::SetUpCommandLine(command_line);
    command_line->AppendSwitch(switches::kEnableExperimentalExtensionApis);
  }

  // SpeechInputExtensionInterface methods.
  virtual bool HasAudioInputDevices() OVERRIDE {
    return recording_devices_available_;
  }

  virtual bool IsCapturingAudio() OVERRIDE {
    // Only the mock recognizer is supposed to be recording during testing.
    return HasValidRecognizer();
  }

  virtual bool HasValidRecognizer() OVERRIDE {
    return recognizer_is_valid_;
  }

  virtual void StartRecording(
      content::SpeechRecognitionEventListener* listener,
      net::URLRequestContextGetter* context_getter,
      const std::string& extension_name,
      const std::string& language,
      const std::string& grammar,
      bool filter_profanities,
      int render_process_id) OVERRIDE;

  virtual void StopRecording(bool recognition_failed) OVERRIDE;

  SpeechInputExtensionManager* GetManager() {
    return manager_.get();
  }

  // Auxiliary class used to hook the API manager into the test during its
  // lifetime. Required since browser() is not available during the set up
  // or tear down callbacks, or during the test class construction.
  class AutoManagerHook {
   public:
    explicit AutoManagerHook(SpeechInputExtensionApiTest* test)
        : test_(test) {
      test_->GetManager()->SetSpeechInputExtensionInterface(test_);
    }

    ~AutoManagerHook() {
      test_->GetManager()->SetSpeechInputExtensionInterface(NULL);
    }

   private:
    SpeechInputExtensionApiTest* test_;
  };

 private:
  void ProvideResults();

  bool recording_devices_available_;
  bool recognizer_is_valid_;
  content::SpeechRecognitionErrorCode next_error_;
  content::SpeechRecognitionResult next_result_;
  int result_delay_ms_;

  scoped_refptr<SpeechInputExtensionManager> manager_;
};

SpeechInputExtensionApiTest::SpeechInputExtensionApiTest()
    : recording_devices_available_(true),
      recognizer_is_valid_(false),
      next_error_(content::SPEECH_RECOGNITION_ERROR_NONE),
      result_delay_ms_(0) {
}

SpeechInputExtensionApiTest::~SpeechInputExtensionApiTest() {
}

void SpeechInputExtensionApiTest::StartRecording(
      content::SpeechRecognitionEventListener* listener,
      net::URLRequestContextGetter* context_getter,
      const std::string& extension_name,
      const std::string& language,
      const std::string& grammar,
      bool filter_profanities,
      int render_process_id) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
  recognizer_is_valid_ = true;

  // Notify that recording started.
  MessageLoop::current()->PostDelayedTask(
      FROM_HERE,
      base::Bind(&SpeechInputExtensionManager::OnAudioStart,
                 GetManager(),
                 kSessionIDForTests),
      base::TimeDelta());

  // Notify sound start in the input device.
  MessageLoop::current()->PostDelayedTask(
      FROM_HERE,
      base::Bind(&SpeechInputExtensionManager::OnSoundStart,
                 GetManager(),
                 kSessionIDForTests),
      base::TimeDelta());

  if (result_delay_ms_ != kDontDispatchCall) {
    // Dispatch the recognition results.
    MessageLoop::current()->PostDelayedTask(
        FROM_HERE,
        base::Bind(&SpeechInputExtensionApiTest::ProvideResults, this),
        base::TimeDelta::FromMilliseconds(result_delay_ms_));
  }
}

void SpeechInputExtensionApiTest::StopRecording(bool recognition_failed) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
  recognizer_is_valid_ = false;
}

void SpeechInputExtensionApiTest::ProvideResults() {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));

  if (next_error_ != content::SPEECH_RECOGNITION_ERROR_NONE) {
    GetManager()->OnRecognitionError(
        kSessionIDForTests, content::SpeechRecognitionError(next_error_));
    return;
  }

  GetManager()->OnSoundEnd(kSessionIDForTests);
  GetManager()->OnAudioEnd(kSessionIDForTests);
  GetManager()->OnRecognitionResult(kSessionIDForTests, next_result_);
  GetManager()->OnRecognitionEnd(kSessionIDForTests);
}

// Every test should leave the manager in the idle state when finished.
IN_PROC_BROWSER_TEST_F(SpeechInputExtensionApiTest, StartStopTest) {
  AutoManagerHook hook(this);

  SetRecognitionDelay(kDontDispatchCall);
  ASSERT_TRUE(RunExtensionTest("speech_input/start_stop")) << message_;
}

IN_PROC_BROWSER_TEST_F(SpeechInputExtensionApiTest, NoDevicesAvailable) {
  AutoManagerHook hook(this);

  SetRecordingDevicesAvailable(false);
  ASSERT_TRUE(RunExtensionTest("speech_input/start_error")) << message_;
}

IN_PROC_BROWSER_TEST_F(SpeechInputExtensionApiTest, RecognitionSuccessful) {
  AutoManagerHook hook(this);

  content::SpeechRecognitionResult result;
  result.hypotheses.push_back(
      content::SpeechRecognitionHypothesis(
          UTF8ToUTF16("this is a test"), 0.99));
  SetRecognitionResult(result);
  ASSERT_TRUE(RunExtensionTest("speech_input/recognition")) << message_;
}

IN_PROC_BROWSER_TEST_F(SpeechInputExtensionApiTest, RecognitionError) {
  AutoManagerHook hook(this);

  SetRecognitionError(content::SPEECH_RECOGNITION_ERROR_NETWORK);
  ASSERT_TRUE(RunExtensionTest("speech_input/recognition_error")) << message_;
}


text:
//
// Created by CaoTao, 2018/03/08
//

/*
 * LeetCode link:
 * https://leetcode.com/problems/maximum-subarray/description/
 */

/**
 * @param {number[]} nums
 * @return {number}
 */
var maxSubArray = function(nums) {
  var length = nums.length
  if (length < 2) {
    return nums[0]
  }

  var maxSum = nums[0], currentMaxSum = nums[0]
  for (var i = 1; i < length; i++) {
    currentMaxSum = currentMaxSum > 0 ? currentMaxSum + nums[i] : nums[i]
    if (currentMaxSum > maxSum) {
      maxSum = currentMaxSum
    }
  }
  return maxSum
};


text:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one
  or more contributor license agreements.  See the NOTICE file
  distributed with this work for additional information
  regarding copyright ownership.  The ASF licenses this file
  to you under the Apache License, Version 2.0 (the
  "License"); you may not use this file except in compliance
  with the License.  You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing,
  software distributed under the License is distributed on an
  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  KIND, either express or implied.  See the License for the
  specific language governing permissions and limitations
  under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <parent>
    <groupId>org.apache.sling</groupId>
    <artifactId>sling</artifactId>
    <version>30</version>
    <relativePath />
  </parent>

  <artifactId>org.apache.sling.repoinit.parser</artifactId>
  <packaging>bundle</packaging>
  <version>1.2.1-SNAPSHOT</version>
  <name>Apache Sling RepoInit Parser</name>
  <description>Parser for the Repository Initialization language used in Sling</description>

  <scm>
    <connection>scm:svn:https://svn.apache.org/repos/asf/sling/trunk/bundles/extensions/repoinit/parser</connection>
    <developerConnection>scm:svn:https://svn.apache.org/repos/asf/sling/trunk/bundles/extensions/repoinit/parser</developerConnection>
    <url>https://svn.apache.org/repos/asf/sling/trunk/bundles/extensions/repoinit/parser</url>
  </scm>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.felix</groupId>
        <artifactId>maven-bundle-plugin</artifactId>
        <extensions>true</extensions>
      </plugin>
      <plugin>
        <groupId>org.apache.rat</groupId>
        <artifactId>apache-rat-plugin</artifactId>
        <configuration>
          <excludes>
            <exclude>src/test/resources/testcases/**</exclude>
          </excludes>
        </configuration>
      </plugin>
       <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>javacc-maven-plugin</artifactId>
        <version>2.5</version>
        <executions>
          <execution>
            <id>calculator</id>
            <configuration>
              <sourceDirectory>${basedir}/src/main/javacc</sourceDirectory>
            </configuration>
            <goals>
              <goal>jjtree-javacc</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-javadoc-plugin</artifactId>
                <configuration>
                    <excludePackageNames>
                        org.apache.sling.repoinit.parser.impl
                    </excludePackageNames>
                </configuration>
            </plugin>
    </plugins>
  </build>

    <dependencies>
        <dependency>
            <groupId>org.osgi</groupId>
            <artifactId>osgi.core</artifactId>
        </dependency>
      <!-- testing -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>


text:
import sys
import logbook
import numpy as np

from zipline.finance import commission

zipline_logging = logbook.NestedSetup([
    logbook.NullHandler(),
    logbook.StreamHandler(sys.stdout, level=logbook.INFO),
    logbook.StreamHandler(sys.stderr, level=logbook.ERROR),
])
zipline_logging.push_application()

STOCKS = ['AMD', 'CERN', 'COST', 'DELL', 'GPS', 'INTC', 'MMM']

# On-Line Portfolio Moving Average Reversion

# More info can be found in the corresponding paper:
# http://icml.cc/2012/papers/168.pdf
def initialize(algo, eps=1, window_length=5):
    algo.stocks = STOCKS
    algo.sids = [algo.symbol(symbol) for symbol in algo.stocks]
    algo.m = len(algo.stocks)
    algo.price = {}
    algo.b_t = np.ones(algo.m) / algo.m
    algo.last_desired_port = np.ones(algo.m) / algo.m
    algo.eps = eps
    algo.init = True
    algo.days = 0
    algo.window_length = window_length

    algo.set_commission(commission.PerShare(cost=0))

def handle_data(algo, data):
    algo.days += 1
    if algo.days < algo.window_length:
        return

    if algo.init:
        rebalance_portfolio(algo, data, algo.b_t)
        algo.init = False
        return

    m = algo.m

    x_tilde = np.zeros(m)
    b = np.zeros(m)

    # find relative moving average price for each asset
    mavgs = data.history(algo.sids, 'price', algo.window_length, '1d').mean()
    for i, sid in enumerate(algo.sids):
        price = data.current(sid, "price")
        # Relative mean deviation
        x_tilde[i] = mavgs[sid] / price

    ###########################
    # Inside of OLMAR (algo 2)
    x_bar = x_tilde.mean()

    # market relative deviation
    mark_rel_dev = x_tilde - x_bar

    # Expected return with current portfolio
    exp_return = np.dot(algo.b_t, x_tilde)
    weight = algo.eps - exp_return
    variability = (np.linalg.norm(mark_rel_dev)) ** 2

    # test for divide-by-zero case
    if variability == 0.0:
        step_size = 0
    else:
        step_size = max(0, weight / variability)

    b = algo.b_t + step_size * mark_rel_dev
    b_norm = simplex_projection(b)
    np.testing.assert_almost_equal(b_norm.sum(), 1)

    rebalance_portfolio(algo, data, b_norm)

    # update portfolio
    algo.b_t = b_norm

def rebalance_portfolio(algo, data, desired_port):
    # rebalance portfolio
    desired_amount = np.zeros_like(desired_port)
    current_amount = np.zeros_like(desired_port)
    prices = np.zeros_like(desired_port)

    if algo.init:
        positions_value = algo.portfolio.starting_cash
    else:
        positions_value = algo.portfolio.positions_value + \
            algo.portfolio.cash

    for i, sid in enumerate(algo.sids):
        current_amount[i] = algo.portfolio.positions[sid].amount
        prices[i] = data.current(sid, "price")

    desired_amount = np.round(desired_port * positions_value / prices)

    algo.last_desired_port = desired_port
    diff_amount = desired_amount - current_amount

    for i, sid in enumerate(algo.sids):
        algo.order(sid, diff_amount[i])

def simplex_projection(v, b=1):
    """Projection vectors to the simplex domain

    Implemented according to the paper: Efficient projections onto the
    l1-ball for learning in high dimensions, John Duchi, et al. ICML 2008.
    Implementation Time: 2011 June 17 by Bin@libin AT pmail.ntu.edu.sg
    Optimization Problem: min_{w}\| w - v \|_{2}^{2}
    s.t. sum_{i=1}^{m}=z, w_{i}\geq 0

    Input: A vector v \in R^{m}, and a scalar z > 0 (default=1)
    Output: Projection vector w

    :Example:
    >>> proj = simplex_projection([.4 ,.3, -.4, .5])
    >>> proj  # doctest: +NORMALIZE_WHITESPACE
    array([ 0.33333333, 0.23333333, 0. , 0.43333333])
    >>> print(proj.sum())
    1.0

    Original matlab implementation: John Duchi (jduchi@cs.berkeley.edu)
    Python-port: Copyright 2013 by Thomas Wiecki (thomas.wiecki@gmail.com).
    """

    v = np.asarray(v)
    p = len(v)

    # Sort v into u in descending order
    v = (v > 0) * v
    u = np.sort(v)[::-1]
    sv = np.cumsum(u)

    rho = np.where(u > (sv - b) / np.arange(1, p + 1))[0][-1]
    theta = np.max([0, (sv[rho] - b) / (rho + 1)])
    w = (v - theta)
    w[w < 0] = 0
    return w

# Note: this function can be removed if running
# this algorithm on quantopian.com
def analyze(context=None, results=None):
    import matplotlib.pyplot as plt
    fig = plt.figure()
    ax = fig.add_subplot(111)
    results.portfolio_value.plot(ax=ax)
    ax.set_ylabel('Portfolio value (USD)')
    plt.show()

def _test_args():
    """Extra arguments to use when zipline's automated tests run this example.
    """
    import pandas as pd

    return {
        'start': pd.Timestamp('2004', tz='utc'),
        'end': pd.Timestamp('2008', tz='utc'),
    }


text:
using System;
using System.Configuration;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNet.Identity;
using Microsoft.Owin.Testing;
using Moq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NUnit.Framework;
using Owin;
using Umbraco.Core;
using Umbraco.Core.Configuration;
using Umbraco.Core.Models.Identity;
using Umbraco.Core.Models.Membership;
using Umbraco.Core.PropertyEditors;
using Umbraco.Core.Security;
using Umbraco.RestApi.Routing;
using Umbraco.RestApi.Security;
using Umbraco.RestApi.Tests.TestHelpers;
using Umbraco.Web.Security.Identity;
using Constants = Umbraco.Core.Constants;

namespace Umbraco.RestApi.Tests
{
    [TestFixture]
    public class AuthorizationServerTests
    {
        [OneTimeSetUp]
        public void FixtureSetUp()
        {
            ConfigurationManager.AppSettings.Set("umbracoPath", "~/umbraco");
            ConfigurationManager.AppSettings.Set("umbracoConfigurationStatus", UmbracoVersion.Current.ToString(3));
        }

        [TearDown]
        public void TearDown()
        {
            //Hack - because Reset is internal
            typeof(PropertyEditorResolver).CallStaticMethod("Reset", true);
            UmbracoRestApiOptionsInstance.Options = new UmbracoRestApiOptions();
        }

        [Test]
        public async Task Get_Token()
        {
            var startup = new TestStartup(
                //This will be invoked before the controller is created so we can modify these mocked services,
                (testServices) =>
                {

                });

            var authServerOptions = new UmbracoAuthorizationServerProviderOptions
            {
                Secret = "abcdefghijklmnopqrstuvwxyz12345678909876543210",
                Audience = "test",
                AllowInsecureHttp = true
            };
            using (var server = TestServer.Create(app =>
            {
                ConfigureUserManager(app, new ClaimsIdentity(new[] {new Claim("test", "test")}), startup.ApplicationContext);
                app.UseUmbracoTokenAuthentication(authServerOptions);
                var httpConfig = startup.UseTestWebApiConfiguration(app);
                app.UseUmbracoRestApi(startup.ApplicationContext, new UmbracoRestApiOptions
                {
                    //customize the authz policies, in this case we want to allow everything
                    CustomAuthorizationPolicyCallback = (policyName, defaultPolicy) => (builder => builder.RequireAssertion(context => true))
                });
                app.UseWebApi(httpConfig);
            }))
            {
                var request = new HttpRequestMessage
                {
                    RequestUri = new Uri($"http://testserver{authServerOptions.AuthEndpoint}"),
                    Method = HttpMethod.Post,
                    Content = new StringContent(
                        "grant_type=password&username=YOURUSERNAME&password=YOURPASSWORD",
                        Encoding.UTF8,
                        "application/x-www-form-urlencoded")
                };

                //add the origin so Cors kicks in!
                request.Headers.Add("Origin", "http://localhost:12061");
                Console.WriteLine(request);
                var result = await server.HttpClient.SendAsync(request);
                Console.WriteLine(result);

                var json = await ((StreamContent)result.Content).ReadAsStringAsync();
                var djson = JsonConvert.DeserializeObject<JObject>(json);
                Console.Write(JsonConvert.SerializeObject(djson, Formatting.Indented));

                Assert.AreEqual(HttpStatusCode.OK, result.StatusCode);

                Assert.IsTrue(result.Headers.Contains("Access-Control-Allow-Origin"));
                var acao = result.Headers.GetValues("Access-Control-Allow-Origin");
                Assert.AreEqual(1, acao.Count());

                //looks like the mvc cors default is to allow the request domain instea of *
                Assert.AreEqual("http://localhost:12061", acao.First());

                Assert.IsNotNull(djson["access_token"].Value<string>());
                Assert.AreEqual("bearer", djson["token_type"].Value<string>());
                Assert.IsNotNull(djson["expires_in"].Value<string>());
            }
        }

        [Test]
        public async Task Use_Token()
        {
            var startup = new TestStartup(
                //This will be invoked before the controller is created so we can modify these mocked services,
                (testServices) =>
                {

                });

            var authServerOptions = new UmbracoAuthorizationServerProviderOptions
            {
                Secret = "abcdefghijklmnopqrstuvwxyz12345678909876543210",
                Audience = "test",
                AllowInsecureHttp = true
            };
            using (var server = TestServer.Create(app =>
            {
                ConfigureUserManager(app,
                    //For published content we can have a normal ClaimsIdentity
                    new ClaimsIdentity(new[]
                    {
                        new Claim(AuthorizationPolicies.UmbracoRestApiClaimType, "true", ClaimValueTypes.Boolean, AuthorizationPolicies.UmbracoRestApiIssuer),

                        new Claim(ClaimTypes.Locality, "en-US", ClaimValueTypes.String, AuthorizationPolicies.UmbracoRestApiIssuer),
                        new Claim(ClaimTypes.GivenName, "Admin", ClaimValueTypes.String, AuthorizationPolicies.UmbracoRestApiIssuer),
                        new Claim(ClaimTypes.Name, "admin@umbraco.com", ClaimValueTypes.String, AuthorizationPolicies.UmbracoRestApiIssuer),
                        new Claim(ClaimTypes.NameIdentifier, "0", ClaimValueTypes.Integer, AuthorizationPolicies.UmbracoRestApiIssuer),
                        new Claim(Constants.Security.AllowedApplicationsClaimType, "content", ClaimValueTypes.String, AuthorizationPolicies.UmbracoRestApiIssuer),
                        new Claim(Constants.Security.StartContentNodeIdClaimType, "[-1]", ClaimValueTypes.String, AuthorizationPolicies.UmbracoRestApiIssuer),
                        new Claim(Constants.Security.StartMediaNodeIdClaimType, "[-1]", ClaimValueTypes.String, AuthorizationPolicies.UmbracoRestApiIssuer),

                    }), startup.ApplicationContext);

                app.UseUmbracoTokenAuthentication(authServerOptions);
                var httpConfig = startup.UseTestWebApiConfiguration(app);
                app.UseUmbracoRestApi(startup.ApplicationContext, new UmbracoRestApiOptions
                {
                    //customize the authz policies, in this case we want to allow everything
                    CustomAuthorizationPolicyCallback = (policyName, defaultPolicy) => (builder => builder.RequireAssertion(context => true))
                });
                app.UseWebApi(httpConfig);
            }))
            {
                var tokenRequest = new HttpRequestMessage
                {
                    RequestUri = new Uri($"http://testserver{authServerOptions.AuthEndpoint}"),
                    Method = HttpMethod.Post,
                    Content = new StringContent(
                        "grant_type=password&username=YOURUSERNAME&password=YOURPASSWORD",
                        Encoding.UTF8,
                        "application/x-www-form-urlencoded")
                };

                //add the origin so Cors kicks in!
                tokenRequest.Headers.Add("Origin", "http://localhost:12061");
                Console.WriteLine(tokenRequest);
                var result = await server.HttpClient.SendAsync(tokenRequest);
                Console.WriteLine(result);
                var json = await ((StreamContent)result.Content).ReadAsStringAsync();
                var djson = JsonConvert.DeserializeObject<JObject>(json);
                Console.Write(JsonConvert.SerializeObject(djson, Formatting.Indented));
                Assert.AreEqual(HttpStatusCode.OK, result.StatusCode);
                var token = djson["access_token"].Value<string>();

                //Now we can use the token

                var accessRequest = new HttpRequestMessage
                {
                    RequestUri = new Uri($"http://testserver/umbraco/rest/v1/{RouteConstants.ContentSegment}"),
                    Method = HttpMethod.Get,
                    Headers = { Authorization = new AuthenticationHeaderValue("Bearer", token) }
                };

                accessRequest.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/hal+json"));
                accessRequest.Headers.Add("Access-Control-Request-Headers", "accept, authorization");
                accessRequest.Headers.Add("Access-Control-Request-Method", "GET");
                accessRequest.Headers.Add("Origin", "http://localhost:12061");
                accessRequest.Headers.Add("Referer", "http://localhost:12061/browser.html");

                Console.WriteLine(accessRequest);
                result = await server.HttpClient.SendAsync(accessRequest);
                Console.WriteLine(result);

                json = await ((StreamContent)result.Content).ReadAsStringAsync();
                Console.Write(JsonConvert.SerializeObject(JsonConvert.DeserializeObject(json), Formatting.Indented));

                Assert.AreEqual(HttpStatusCode.OK, result.StatusCode);

            }
        }

        private void ConfigureUserManager(IAppBuilder app, ClaimsIdentity identity, ApplicationContext appCtx)
        {
            var claimsFactory = new Mock<IClaimsIdentityFactory<BackOfficeIdentityUser, int>>();
            claimsFactory.Setup(x => x.CreateAsync(It.IsAny<UserManager<BackOfficeIdentityUser, int>>(), It.IsAny<BackOfficeIdentityUser>(), It.IsAny<string>()))
                .Returns(Task.FromResult(identity));
            var userStore = Mock.Of<IUserStore<BackOfficeIdentityUser, int>>();
            var backOfficeUserManager = new Mock<BackOfficeUserManager>(userStore);
            backOfficeUserManager.Setup(x => x.FindAsync(It.IsAny<string>(), It.IsAny<string>()))
                .Returns(Task.FromResult(new BackOfficeIdentityUser(1, new IReadOnlyUserGroup[] { })));
            backOfficeUserManager.Object.ClaimsIdentityFactory = claimsFactory.Object;

            app.ConfigureUserManagerForUmbracoBackOffice<BackOfficeUserManager, BackOfficeIdentityUser>(
                appCtx,
                (options, context) => backOfficeUserManager.Object);
        }
    }
}