Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 45 lines (35 sloc) 1.059 kb
8d70150 @avsm Add an ABNF parser and a helper command-line utility to help visualize
avsm authored
1 (** Copyright (c) 2008,2009 Anil Madhavapeddy <anil@recoil.org>
2 ** See the COPYING file included in this distribution for licensing details *)
3
4 open Lexing
5 open Printf
6
7 (* Keep track of our location *)
8 type t = {
9 file_name: string option;
10 line_num: int;
11 column_num: int;
12 }
13
14 let start = ref 1
15 let line = ref 1
16 let file_name = ref None
17
18 let initial_location = { file_name=None; line_num=0; column_num=0 }
19 let current_location = ref initial_location
20
21 let start_parse f =
22 start := 1;
23 line := 1;
24 file_name := Some f
25
26 let new_line lexbuf =
27 start := lexeme_end lexbuf;
28 incr line
29
30 let next_token lexbuf =
31 let col = lexeme_start lexbuf - !start in
32 let l = {file_name= !file_name; line_num= !line; column_num=col} in
33 current_location := l;
34 l
35
36 let string_of_location l =
37 match l.file_name with
38 |None -> ":"
39 |Some f ->
40 let c = if l.column_num > 0 then sprintf " char %d" l.column_num else
41 "" in
42 sprintf " at line %d%s:" l.line_num c
43
44 exception Parse_failure of (t * string)
Something went wrong with that request. Please try again.