Permalink
Browse files

Moved namespace from DevHawk.Parser to Cashel and extracted primitive…

…s into different modules to enable better dependency management.
  • Loading branch information...
1 parent 251c2e0 commit ec161306111677774a73f014c8a55b04db553a02 @panesofglass panesofglass committed Mar 6, 2011
View
11 src/Cashel.Peg/peg.fs
@@ -40,7 +40,7 @@ EndOfFile <- !.
*)
-namespace DevHawk.Parser
+namespace Cashel
module Peg =
@@ -142,8 +142,8 @@ module Peg =
//---------------------------------------------------------------------------------------------
- open DevHawk.Parser.Core
- open DevHawk.Parser.Primitives
+ open Cashel
+ open Cashel.ListPrimitives
///EndOfFile <- !.
let EndOfFile = eof
@@ -332,7 +332,4 @@ module Peg =
return {name=id;exp=ex} }
///Grammar <- Spacing Definition+ EndOfFile
- let Grammar = Spacing >>. repeat1 Definition .>> EndOfFile
-
-
-
+ let Grammar = Spacing >>. repeat1 Definition .>> EndOfFile
View
7 src/Cashel.Peg/peg2.fs
@@ -52,7 +52,8 @@ EndOfFile <- !.
*)
-namespace DevHawk.Parser
+namespace Cashel
+
module Peg2 =
//---------------------------------------------------------------------------------------------
@@ -192,8 +193,8 @@ module Peg2 =
//---------------------------------------------------------------------------------------------
- open DevHawk.Parser.Core
- open DevHawk.Parser.Primitives
+ open Cashel
+ open Cashel.ListPrimitives
let _EndOfFile = !~ item
View
2 src/Cashel.Tests/parser_test.fs
@@ -3,7 +3,7 @@ module ParserTest
open NUnit.Framework
open FsUnit
-open DevHawk.Parser.Core
+open Cashel
let (!!) str = List.ofSeq str
let hello = !!"hello"
View
3 src/Cashel.Tests/peg2_test.fs
@@ -48,7 +48,8 @@ PEG2
open NUnit.Framework
open FsUnit
-open DevHawk.Parser.Peg2
+open Cashel
+open Cashel.Peg2
let (>|>) act exp =
match exp with
View
27 src/Cashel.Tests/peg_test.fs
@@ -6,7 +6,8 @@ let chr (c:int) = System.Convert.ToChar(c)
open NUnit.Framework
open FsUnit
-open DevHawk.Parser.Peg
+open Cashel
+open Cashel.Peg
[<Test>]
let test_EndOfFile_with_empty_string () =
@@ -203,51 +204,51 @@ let test_Range_single_fail () =
[<Test>]
let test_Class_single () =
- DevHawk.Parser.Peg.Class !! "[a]test" |> should equal (Some([Single('a')], !!"test"))
+ Cashel.Peg.Class !! "[a]test" |> should equal (Some([Single('a')], !!"test"))
[<Test>]
let test_Class_single_spacing () =
- DevHawk.Parser.Peg.Class !! "[a]\t\ttest" |> should equal (Some([Single('a')], !!"test"))
+ Cashel.Peg.Class !! "[a]\t\ttest" |> should equal (Some([Single('a')], !!"test"))
[<Test>]
let test_Class_single_range () =
- DevHawk.Parser.Peg.Class !! "[a-z]test" |> should equal (Some([Dual('a', 'z')], !!"test"))
+ Cashel.Peg.Class !! "[a-z]test" |> should equal (Some([Dual('a', 'z')], !!"test"))
[<Test>]
let test_Class_multiple () =
- DevHawk.Parser.Peg.Class !! "[ab-z]test" |> should equal (Some([Single('a');Dual('b','z')], !!"test"))
+ Cashel.Peg.Class !! "[ab-z]test" |> should equal (Some([Single('a');Dual('b','z')], !!"test"))
[<Test>]
let test_Class_failure_no_end_bracket () =
- DevHawk.Parser.Peg.Class !! "[ab-ztest" |> should equal None
+ Cashel.Peg.Class !! "[ab-ztest" |> should equal None
[<Test>]
let test_Class_failure () =
- DevHawk.Parser.Peg.Class !! "ab-z]test" |> should equal None
+ Cashel.Peg.Class !! "ab-z]test" |> should equal None
[<Test>]
let test_Literal_single_quote () =
- DevHawk.Parser.Peg.Literal !! "'test' me" |> should equal (Some(!!"test", !!"me"))
+ Cashel.Peg.Literal !! "'test' me" |> should equal (Some(!!"test", !!"me"))
[<Test>]
let test_Literal_double_quote () =
- DevHawk.Parser.Peg.Literal !! "\"test\" me" |> should equal (Some(!!"test", !!"me"))
+ Cashel.Peg.Literal !! "\"test\" me" |> should equal (Some(!!"test", !!"me"))
[<Test>]
let test_Literal_no_end_quote () =
- DevHawk.Parser.Peg.Literal !! "\"test me" |> should equal None
+ Cashel.Peg.Literal !! "\"test me" |> should equal None
[<Test>]
let test_Identifier () =
- DevHawk.Parser.Peg.Identifier !! "tE_s9t me" |> should equal (Some(!!"tE_s9t", !!"me"))
+ Cashel.Peg.Identifier !! "tE_s9t me" |> should equal (Some(!!"tE_s9t", !!"me"))
[<Test>]
let test_Identifier_start_with_underscore () =
- DevHawk.Parser.Peg.Identifier !! "_9test me" |> should equal (Some(!!"_9test", !!"me"))
+ Cashel.Peg.Identifier !! "_9test me" |> should equal (Some(!!"_9test", !!"me"))
[<Test>]
let test_Identifier_fail_start_with_number () =
- DevHawk.Parser.Peg.Identifier !! "9test me" |> should equal None
+ Cashel.Peg.Identifier !! "9test me" |> should equal None
[<Test>]
let test_Primary_Identifier () =
View
8 src/Cashel.Tests/primitives_test.fs
@@ -5,7 +5,7 @@ let (!!) str = List.ofSeq str
open NUnit.Framework
open FsUnit
-open DevHawk.Parser.Primitives
+open Cashel
[<Test>]
let test_item () =
@@ -40,6 +40,9 @@ let test_satisy_simple_predicate () =
let test_satisy_failure_predicate () =
satisfy item (fun x -> x = 'e') !!"test" |> should equal None
+
+open Cashel.ListPrimitives
+
[<Test>]
let test_anyOf_success_predicate () =
anyOf ['q'..'v'] !!"test" |> should equal (Some('t', !!"est"))
@@ -174,6 +177,9 @@ let test_skipItems () =
let test_skipItems_fail () =
skipItems !!"ts" !!"test" |> should equal None
+
+open Cashel.CharListPrimitives
+
[<Test>]
let test_space () =
let exp = Some (' ', !!" test")
View
2 src/Cashel/Cashel.fsproj
@@ -46,6 +46,8 @@
<ItemGroup>
<Compile Include="parser.fs" />
<Compile Include="primitives.fs" />
+ <Compile Include="ListPrimitives.fs" />
+ <Compile Include="CharListPrimitives.fs" />
</ItemGroup>
<Import Project="..\..\lib\FSharp\Microsoft.FSharp.Targets" />
</Project>
View
23 src/Cashel/CharListPrimitives.fs
@@ -0,0 +1,23 @@
+namespace Cashel
+module CharListPrimitives =
+ open Cashel
+
+ //-------------------------char list primitives-------------------------------------------
+
+ let addLineAndCol cl =
+ let rec worker cl line col =
+ match cl with
+ | '\r'::'\n'::tail -> ('\r', line, col)::('\n', line, (col+1))::(worker tail (line+1) 1)
+ | '\n'::tail -> ('\n', line, col)::(worker tail (line+1) 1)
+ | '\r'::tail -> ('\r', line, col)::(worker tail (line+1) 1)
+ | head::tail -> (head, line, col)::(worker tail line (col+1))
+ | [] -> []
+ worker cl 1 1
+
+ let lowercase = anyOf ['a'..'z']
+ let uppercase = anyOf ['A'..'Z']
+ let letter = lowercase +++ uppercase
+ let digit = anyOf ['0'..'9']
+ let digitval = digit >>= (fun d -> result (int d - int '0'))
+ let space = satisfy item (System.Char.IsWhiteSpace)
+ let skipSpace = repeat space |> forget
View
38 src/Cashel/ListPrimitives.fs
@@ -0,0 +1,38 @@
+namespace Cashel
+
+[<AutoOpen>]
+module ListPrimitives =
+ open Cashel
+
+ //-------------------------List primitives-------------------------------------------
+ ///item assumes the input is a list and returns a tuple of the head and tail
+ let item : Parser<'a list, 'a> =
+ fun input ->
+ match input with
+ | x::xs -> Some(x,xs)
+ | [] -> None
+
+ ///eof checks that we're at the end of the list being parsed
+ let eof : Parser<'a list, unit> =
+ fun input ->
+ match input with
+ | [] -> Some((), [])
+ | _ -> None
+
+ ///anyOf checks the value at the start of the input is in the list of items l
+ let anyOf l = satisfy item (fun x -> l |> List.exists (fun y -> x = y))
+
+ ///itemEqual checks the value at the start of the input matches the value v
+ let itemEqual v = satisfy item (fun x -> x = v)
+
+ ///itemsEqual recursively uses itemEqual to check to see if a list of values l matches the start of the input
+ let rec itemsEqual l =
+ match l with
+ | [] -> result []
+ | x::xs -> itemEqual x >>= (fun i -> itemsEqual xs >>= (fun is -> result (i::is)))
+
+ ///skipItem calls itemEqual but tosses the parse value
+ let skipItem v = itemEqual v |> forget
+
+ ///skipItems calls itemsEqual but tosses the parse value
+ let skipItems l = itemsEqual l |> forget
View
5 src/Cashel/parser.fs
@@ -1,6 +1,7 @@
-namespace DevHawk.Parser
+namespace Cashel
-module Core =
+[<AutoOpen>]
+module Parser =
type Parser<'input, 'result> = 'input -> ('result * 'input) option
View
64 src/Cashel/primitives.fs
@@ -1,9 +1,9 @@
-namespace DevHawk.Parser
+namespace Cashel
+[<AutoOpen>]
module Primitives =
-
- open DevHawk.Parser.Core
-
+ open Cashel
+
//-------------------------Basic primitives----------------------------------------------------
//These primitives make no assumption as to the basic types of the parser input or result types
@@ -54,58 +54,4 @@ module Primitives =
///repeatUntil calls p1 repeatedly until p2 succeeds
let repeatUntil p1 p2 = repeat (!~ p2 >>. p1) .>> p2
-
-
- //-------------------------List primitives-------------------------------------------
- ///item assumes the input is a list and returns a tuple of the head and tail
- let item : Parser<'a list, 'a> =
- fun input ->
- match input with
- | x::xs -> Some(x,xs)
- | [] -> None
-
- ///eof checks that we're at the end of the list being parsed
- let eof : Parser<'a list, unit> =
- fun input ->
- match input with
- | [] -> Some((), [])
- | _ -> None
-
- ///anyOf checks the value at the start of the input is in the list of items l
- let anyOf l = satisfy item (fun x -> l |> List.exists (fun y -> x = y))
-
- ///itemEqual checks the value at the start of the input matches the value v
- let itemEqual v = satisfy item (fun x -> x = v)
-
- ///itemsEqual recursively uses itemEqual to check to see if a list of values l matches the start of the input
- let rec itemsEqual l =
- match l with
- | [] -> result []
- | x::xs -> itemEqual x >>= (fun i -> itemsEqual xs >>= (fun is -> result (i::is)))
-
- ///skipItem calls itemEqual but tosses the parse value
- let skipItem v = itemEqual v |> forget
-
- ///skipItems calls itemsEqual but tosses the parse value
- let skipItems l = itemsEqual l |> forget
-
-
- //-------------------------char list primitives-------------------------------------------
-
- let addLineAndCol cl =
- let rec worker cl line col =
- match cl with
- | '\r'::'\n'::tail -> ('\r', line, col)::('\n', line, (col+1))::(worker tail (line+1) 1)
- | '\n'::tail -> ('\n', line, col)::(worker tail (line+1) 1)
- | '\r'::tail -> ('\r', line, col)::(worker tail (line+1) 1)
- | head::tail -> (head, line, col)::(worker tail line (col+1))
- | [] -> []
- worker cl 1 1
-
- let lowercase = anyOf ['a'..'z']
- let uppercase = anyOf ['A'..'Z']
- let letter = lowercase +++ uppercase
- let digit = anyOf ['0'..'9']
- let digitval = digit >>= (fun d -> result (int d - int '0'))
- let space = satisfy item (System.Char.IsWhiteSpace)
- let skipSpace = repeat space |> forget
+

0 comments on commit ec16130

Please sign in to comment.