Skip to content

Instantly share code, notes, and snippets.

View dmajda's full-sized avatar

David Majda dmajda

View GitHub Profile
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|
@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
@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: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
# 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