Skip to content

Instantly share code, notes, and snippets.

View dmajda's full-sized avatar

David Majda dmajda

View GitHub Profile
# Syntax of Ruby is flexible enough to allow writing Lisp-like code.
# We need few helper functions (no Lisp yet):
def list(*args)
args
end
def car(list)
list.first
@dmajda
dmajda / gist:1926638
Created February 27, 2012 19:58
Semantic predicates and label visibility in PEG.js
/*
* Task: Parse even numbers in PEG.js (http://pegjs.majda.cz).
*
* Solution: Let's parse all numbers and reject odd ones using a semantic
* predicate -- an arbitrary piece of code that returns true (meaning "continue
* parsing") or false (meaning "halt parsing").
*
* This solution wouldn't work before commit a2af1fe612 because predicates
* didn't have access to labeled expressions in the grammar as variables
* (without ugly workarounds). But they have the access now and the solution
@dmajda
dmajda / gist:2421891
Created April 19, 2012 15:49
Simple query language parser
{
function makeNode(type, head, tail) {
return tail.length > 0
? {
type: type,
terms: [head].concat(tail.map(function(t) { return t[1]; }))
}
: head;
}
}
@dmajda
dmajda / gist:3010091
Created June 28, 2012 09:07
Regexp parsing proposal for Machete (snippet)
[
:REGEXP,
/^
\/
(
\\ # escape
(
[\\"ntrfvaebs] # one-character escape
|
[0-7]{1,3} # octal number escape
def check_classes
# Get list of all subclasses of Scanny::Checks::Check.
classes = []
ObjectSpace.each_object(Class) do |klass|
classes << klass if klass < Scanny::Checks::Check
end
# Filter out classes that are a superclass of some other class in the list.
# This way only "leaf" classes remain.
classes.reject do |klass|
inuit ~ » free
total used free shared buffers cached
Mem: 8178344 2183072 5995272 0 116532 621840
-/+ buffers/cache: 1444700 6733644
Swap: 15999996 0 15999996
Call
= id:ID params:Params+ {
var result = {
type: "var",
name: id
};
for (var i = 0; i < params.length; i++) {
result = {
type: "call",
@dmajda
dmajda / gist:7688943
Created November 28, 2013 08:49
Simple PEG.js grammar to parse a markup language with nested elements (like XML)
Content =
(Element / Text)*
Element =
startTag:StartTag content:Content endTag:EndTag {
if (startTag != endTag) {
throw new Error(
"Expected </" + startTag + "> but </" + endTag + "> found."
);
}
@dmajda
dmajda / README.md
Last active August 29, 2015 14:18 — forked from nbergseng/README.md

Juttle Wordcloud

This gist adapts a wordcloud visualization from http://www.jasondavies.com/wordcloud/ as a Juttle view, adding transitions between batches for updates.

As a silly little showcase, the demonstration program calculates a frequency count of words from Dr Seuss' "The Cat in the Hat" (text pulled from http://paulandlizdavies.com/poems/cat.htm) by splitting the words from each page into separate points, then counting the words by frequency and showing them in the wordcloud and a barchart.

@dmajda
dmajda / error-stack-test.js
Created June 12, 2015 20:19
Tests for code that adds the stack property to an Error subclass
/* See https://github.com/pegjs/pegjs/pull/342 */
function subclass(child, parent) {
function ctor() { this.constructor = child; }
ctor.prototype = parent.prototype;
child.prototype = new ctor();
}
function StackError() {
if (typeof Error.captureStackTrace !== "function") {