Skip to content

Commit

Permalink
add Parse method
Browse files Browse the repository at this point in the history
  • Loading branch information
robertluo committed Jul 31, 2019
1 parent 1a021c1 commit 8bd0632
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 6 deletions.
15 changes: 15 additions & 0 deletions Edn.Net.sln
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{412558C6-F
EndProject
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Edn.Net.Test", "test\Edn.Net.Test\Edn.Net.Test.fsproj", "{6CA2C52D-D3FC-427E-9608-19D34BC3B74E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Trial", "src\Trial\Trial.csproj", "{ADBC7B25-D0E0-4E0A-98B2-57FD502E9594}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -48,9 +50,22 @@ Global
{6CA2C52D-D3FC-427E-9608-19D34BC3B74E}.Release|x64.Build.0 = Release|Any CPU
{6CA2C52D-D3FC-427E-9608-19D34BC3B74E}.Release|x86.ActiveCfg = Release|Any CPU
{6CA2C52D-D3FC-427E-9608-19D34BC3B74E}.Release|x86.Build.0 = Release|Any CPU
{ADBC7B25-D0E0-4E0A-98B2-57FD502E9594}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{ADBC7B25-D0E0-4E0A-98B2-57FD502E9594}.Debug|Any CPU.Build.0 = Debug|Any CPU
{ADBC7B25-D0E0-4E0A-98B2-57FD502E9594}.Debug|x64.ActiveCfg = Debug|Any CPU
{ADBC7B25-D0E0-4E0A-98B2-57FD502E9594}.Debug|x64.Build.0 = Debug|Any CPU
{ADBC7B25-D0E0-4E0A-98B2-57FD502E9594}.Debug|x86.ActiveCfg = Debug|Any CPU
{ADBC7B25-D0E0-4E0A-98B2-57FD502E9594}.Debug|x86.Build.0 = Debug|Any CPU
{ADBC7B25-D0E0-4E0A-98B2-57FD502E9594}.Release|Any CPU.ActiveCfg = Release|Any CPU
{ADBC7B25-D0E0-4E0A-98B2-57FD502E9594}.Release|Any CPU.Build.0 = Release|Any CPU
{ADBC7B25-D0E0-4E0A-98B2-57FD502E9594}.Release|x64.ActiveCfg = Release|Any CPU
{ADBC7B25-D0E0-4E0A-98B2-57FD502E9594}.Release|x64.Build.0 = Release|Any CPU
{ADBC7B25-D0E0-4E0A-98B2-57FD502E9594}.Release|x86.ActiveCfg = Release|Any CPU
{ADBC7B25-D0E0-4E0A-98B2-57FD502E9594}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{801C9BEE-B3C0-4316-9DD9-918B3C0BB2C7} = {CE2115A8-CB37-4BC5-BCDC-40CFE0E63AF4}
{6CA2C52D-D3FC-427E-9608-19D34BC3B74E} = {412558C6-F141-40B2-B08A-8BE09CBF255B}
{ADBC7B25-D0E0-4E0A-98B2-57FD502E9594} = {CE2115A8-CB37-4BC5-BCDC-40CFE0E63AF4}
EndGlobalSection
EndGlobal
16 changes: 12 additions & 4 deletions src/Edn.Net/Library.fs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
namespace Robertluo

open FParsec

type Keyword =
{ Ns : string option
Name : string }
Expand All @@ -9,7 +11,7 @@ type Keyword =
| { Ns = Some ns; Name = name } -> ns.ToString() + "/" + name.ToString()

type Edn =
| EString of string
| EString of string ///String value
| EInteger of int64
| EBigInt of bigint
| EFloat of float
Expand Down Expand Up @@ -53,10 +55,9 @@ type Edn =
|> sprintf "{%s}"
| EUuid uuid -> "#uuid \"" + uuid.ToString() + "\""
| EInstant dt -> "#instant \"" + dt.ToString() + "\""

[<RequireQualifiedAccessAttribute>]
module Edn =
open FParsec
open System
open System.Xml
open System.Numerics
Expand Down Expand Up @@ -196,4 +197,11 @@ module Edn =
esymbol; estring; etagged ] .>> ws

//-------------- Interface ---------------
let Parse = run evalue
let parse = run evalue

//-------------- Attach functions to type --------------
type Edn with
static member Parse str =
match Edn.parse str with
| Success(v, _, _) -> v
| Failure(msg, _, _) -> failwith msg
14 changes: 14 additions & 0 deletions src/Trial/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Robertluo;
using static System.Console;

namespace Trial
{
class Program
{
static void Main(string[] args)
{
var v = Edn.Parse("{:foo/bar 35 :name \"Hello\"}");
WriteLine("Hello World! " + v.IsEBigInt);
}
}
}
12 changes: 12 additions & 0 deletions src/Trial/Trial.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<ItemGroup>
<ProjectReference Include="..\Edn.Net\Edn.Net.fsproj" />
</ItemGroup>

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.0</TargetFramework>
</PropertyGroup>

</Project>
11 changes: 9 additions & 2 deletions test/Edn.Net.Test/Tests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ open FParsec
open System

let testParse input expected comment =
match Edn.Parse input with
match Edn.parse input with
| Success(actual, _, _) -> Expect.equal actual expected comment
| Failure(msg, _, _) -> failtest msg

Expand Down Expand Up @@ -94,7 +94,7 @@ let tests =
testParse input expected "should run"
testCase "tagged instant" <| fun _ ->
let input = "#inst \"1985-04-12T23:20:50.52Z\""
match Edn.Parse input with
match Edn.parse input with
| Success(EInstant v, _, _) ->
Expect.equal v.Year 1985 "should readable"
| v -> failtestf "not understandable: %A" v ]
Expand Down Expand Up @@ -124,3 +124,10 @@ let test2 =

let s = input.ToString()
Expect.equal s "{:a nil, :foo/bar [35.1, false]}" "" ]

[<Tests>]
let testMisc =
testList "Misc"
[ testCase "Parse static method" <| fun _ ->
let input = "3"
Expect.equal (Edn.Parse input) (EInteger 3L) "ok"]

0 comments on commit 8bd0632

Please sign in to comment.