Ver código fonte

parser: make ast independence 1

gretmn102 3 anos atrás
pai
commit
573720fac0
4 arquivos alterados com 20 adições e 10 exclusões
  1. 14 7
      QSParse/Ast.fs
  2. 2 2
      QSParse/Parsec.fs
  3. 3 0
      QSParse/ParserGeneric.fs
  4. 1 1
      Test/Test.fs

+ 14 - 7
QSParse/Ast.fs

@@ -2,15 +2,22 @@ module Qsp.Ast
 open FsharpMyExtension
 open Qsp
 
-type NoEqualityPosition(pos:FParsec.Position) =
+[<Struct>]
+type Position = { StreamName:string; Index:int64; Line:int64; Column:int64 }
+let positionCreate streamName index line column =
+    { StreamName = streamName; Index = index; Line = line; Column = column }
+let positionEmpty =
+    positionCreate "" 0L 0L 0L
+
+type NoEqualityPosition(pos:Position) =
     member __.Pos = pos
     override __.ToString() = pos.ToString()
     override __.Equals _ = true
     override __.GetHashCode() = 0
 
 let test () =
-    let x = NoEqualityPosition(FParsec.Position("", 0L, 0L, 0L))
-    let y = NoEqualityPosition(FParsec.Position("", 0L, 0L, 1L))
+    let x = NoEqualityPosition(positionCreate "" 0L 0L 0L)
+    let y = NoEqualityPosition(positionCreate "" 0L 0L 1L)
     x = y
 
 [<Struct>]
@@ -75,7 +82,7 @@ module Op =
         Reflection.Reflection.unionEnum<Op>
         |> Array.map (fun x ->
             let IsBinOpSymbolic opName =
-                not <| String.exists FParsec.CharParsers.isLetter opName
+                not <| String.exists System.Char.IsLetter opName
                 : IsBinOpSymbolic
             let y = toString x
             x, (y, IsBinOpSymbolic y) )
@@ -198,7 +205,7 @@ type Location = Location of LocationName * PosStatement list
 //     match x, y with
 //     | StaticStmts xs, StaticStmts ys ->
 //         List.forall2 posStmtEqual xs ys
-//     | _, Raw _ 
+//     | _, Raw _
 //     | _, StaticStmts _ -> x = y
 // and lineKindEqual x y =
 //     match x, y with
@@ -216,12 +223,12 @@ type Location = Location of LocationName * PosStatement list
 //         List.forall2 lineEqual lines lines2
 //     | _, String _
 //     | _, Int _ -> x = y
-// and exprEqual x y = 
+// and exprEqual x y =
 //     match x, y with
 //     | Val x, Val y ->
 //         valueEqual x y
 //     | _, Val _
-//     | _, Arr _ 
+//     | _, Arr _
 //     | _, Expr _
 //     | _, Func _
 //     | _, UnarExpr _

+ 2 - 2
QSParse/Parsec.fs

@@ -332,7 +332,7 @@ let pstmt =
             let p =
                 ws .>>? skipNewline >>. spaces >>. pstmts .>> setIsEndOptionalTo false
                 <|> (spaces >>. pInlineStmts .>> setIsEndOptionalTo true)
-            many1 ((getPosition |>> NoEqualityPosition) .>>.? pelseifHeader .>>. p)
+            many1 ((getPosition |>> (fparsecPosToPos >> NoEqualityPosition)) .>>.? pelseifHeader .>>. p)
             .>>. (pElse1 <|> (pend >>% []))
             |>> fun (elifs, elseBody) ->
                 let rec f = function
@@ -371,7 +371,7 @@ let pstmt =
             notFollowedBy (pchar '-' >>. ws >>. (skipNewline <|> skipChar '-' <|> eof)) // `-` завершает локацию
             >>. (pexpr |>> fun arg -> Proc("*pl", [arg]))
         ]
-    pstmtRef := (getPosition |>> NoEqualityPosition) .>>.? p
+    pstmtRef := (getPosition |>> (fparsecPosToPos >> NoEqualityPosition)) .>>.? p
     pstmt
 
 let pstmts = pstmts' pstmt

+ 3 - 0
QSParse/ParserGeneric.fs

@@ -4,6 +4,9 @@ open FsharpMyExtension
 open FsharpMyExtension.Either
 open Qsp
 
+let fparsecPosToPos (pos:FParsec.Position) =
+    Ast.positionCreate pos.StreamName pos.Index pos.Line pos.Column
+
 let runEither p str =
     match run p str with
     | Success(x, _, _) -> Right x

+ 1 - 1
Test/Test.fs

@@ -255,7 +255,7 @@ let stringLiteralTest =
         testCase "braces escaped" <| fun () ->
             Assert.Equal("", Right "}", runEither stringLiteral "{}}}")
     ]
-let emptyPos = NoEqualityPosition(Position("", 0L, 0L, 0L))
+let emptyPos = NoEqualityPosition positionEmpty
 let emptyPoss x = x |> List.map (fun x -> emptyPos, x)
 let StaticStmts x =
     emptyPoss x