Skip to content

Instantly share code, notes, and snippets.

Less(Optional(Less(Less(OneOf([Less(Literal('0'), Literal('0'))]), Literal('\U000523a9')), Less(Less(Sequence([Literal('0')]), OneOf([Literal('0')])), Sequence([Optional(Literal('0')), Less(Literal('0'), Literal('0')), OneOf([Literal('0')]), Range('0', '0'), Sequence([Literal('0')]), Optional(Literal('0')), Sequence([Literal('0')]), OneOf([Literal('0')]), Optional(Literal('0')), OneOf([Literal('0')])])))), OneOf([Many(Many(Range(' ', ','))), Optional(OneOf([OneOf([Literal('0'), Literal('0'), Literal('0'), Literal('0'), Literal('0')]), Literal('\x0c'), Sequence([Literal('0'), Literal('0'), Literal('0'), Literal('0'), Literal('0'), Literal('0'), Literal('0'), Literal('0'), Literal('0'), Literal('0'), Literal('0'), Literal('0'), Literal('0'), Literal('0'), Literal('0'), Literal('0')]), Any(), Sequence([Literal('0'), Literal('0')]), Range('%', '\U000a562f')])), Sequence([Any(), Sequence([OneOf([Literal('0')]), Many(Literal('0')), Range('0', '0'), Many(Literal('0')), Sequence([Literal('0')]), Many(Literal('0')), S
>>> from grhammer import *
>>> math = Grammar(lambda math: {
... 'number': Sequence([Range('1', '9'), Many(Range('0', '9'))]),
... 'addition': Sequence([math.number, Literal('+'), math.number]),
... })
>>> print(math)
addition: ( number, '+', number ) ;
number: ( '1'..'9', { '0'..'9' } ) ;
>>> math.addition.parse("1+3abc")
ParseOk([['1', []], '+', ['3', []]], 'abc')
math = Grammer({
number: Sequence([Range('1', '9'), Many(Range('0', '9'))]),
addition: Sequence([number, Literal('+'), number]),
})
>>> from parse import *
>>> number = Sequence([Range('1', '9'), Many(Range('0', '9'))])
>>> print(number)
( '1'-'9' [ '0'-'9' ] )
>>> number.parse("1234 hello!")
ParseOk(['1', ['2', '3', '4']], ' hello!')
>>> number.parse("0123")
ParseError("Expected '1'-'9' found '0'")
{- fun '\EOT' getChar -}
fun :: (Monad m, Eq a) => a -> m a -> m [a]
fun t f = do
x <- f
if x == t
then return []
else fmap (x :) (fun t f)
import scala.util.{Either, Left, Right}
import scala.annotation.tailrec
import scala.scalanative.posix.unistd
import scala.scalanative.unsafe._
import scala.scalanative.libc
import libc.errno.errno
import libc.string.strerror
@acdimalev
acdimalev / Main.hs
Created July 23, 2019 09:15
parsing PC Screen Font files
module Main where
import Text.Parsec
import qualified Data.ByteString as B
import qualified Data.ByteString.Char8 as C
magic :: Parsec B.ByteString () ()
magic = string "\x72\xb5\x4a\x86" >> return ()
main :: IO ()
@acdimalev
acdimalev / dirrec.py
Created July 6, 2019 08:50
Fun with directory recursion and Python
fix = lambda f: lambda x: f(fix(f), x)
# presume Python's obsession with iterators...
join = __import__('itertools').chain.from_iterable
bind = lambda f, xs: join(map(f, xs))
# I/O primitives...
quoted = lambda s: '"' + str.replace(s, '"', '\\"') + '"'
unquoted = lambda s: str.replace(s[1:-1], '\\"', '"')
slash = lambda s: str.replace(s, '\\', '\\\\')
unslash = lambda s: str.replace(s, '\\\\', '\\')
shell_escape = lambda s: quoted(slash(s))
shell_unescape = lambda s: unslash(unquoted(s))
#include <asoundlib.h>
int main() {
int retcode, dir;
char name[32];
for (int card = -1; snd_card_next(&card) == 0 && card >= 0;)
{ sprintf(name, "hw:%d", card);
snd_ctl_t *ctl;
snd_ctl_open(&ctl, name, 0);