Skip to content

Instantly share code, notes, and snippets.

@jdh30
jdh30 / JsonParser.fs
Last active January 30, 2024 14:06
Simple JSON parser written in F# using active patterns
type Json =
| Null
| Bool of bool
| Number of float
| String of string
| Array of Json list
| Object of (string * Json) list
type Bracket = Open | Close

In this tutorial we're going to build a set of parser combinators.

What is a parser combinator?

We'll answer the above question in 2 steps.

  1. What is a parser?
  2. and, what is a parser combinator?

So first question: What is parser?

@jdh30
jdh30 / FemtoMLParserUsingActivePatterns.fs
Last active May 22, 2019 05:12
FemtoML parser in F# using active patterns
> let alpha = set['a'..'z'] + set['A'..'Z'];;
> let num = set['0'..'9'];;
> let alphanum = alpha + num;;
> let (|Char|_|) alphabet = function
| c::cs when Set.contains c alphabet -> Some(c, cs)
| _ -> None;;
@jdh30
jdh30 / FemtoMLParserUsingFSLexAndYacc.fs
Created April 15, 2017 22:06
FemtoML parser in F# using fslex and fsyacc
{
open Parse
open Lexing
let ident = function
| "let" -> LET
| "rec" -> REC
| "in" -> IN
| "fun" -> FUN
| "if" -> IF
| "then" -> THEN
@jdh30
jdh30 / FemtoMLParserUsingFParsec.fs
Last active June 6, 2020 03:48
FemtoML parser in F# using FParsec
> let pInt : Parser<_, unit> = puint32 |>> int .>> spaces;;
> let str s = pstring s >>. spaces;;
> let keywords =
"if then else let rec in fun".Split ' '
|> set;;
> let pIdent : Parser<_, unit> =
@amitchhajer
amitchhajer / Count Code lines
Created January 5, 2013 11:08
Count number of code lines in git repository per user
git ls-files -z | xargs -0n1 git blame -w | perl -n -e '/^.*\((.*?)\s*[\d]{4}/; print $1,"\n"' | sort -f | uniq -c | sort -n