/fparsec/main/FParsec/OperatorPrecedenceParser.fsi

http://github.com/sandersn/fing · F# · 140 lines · 35 code · 24 blank · 81 comment · 0 complexity · 7eae2754d045b6fc9669907c85cb35d7 MD5 · raw file

  1. // Copyright (c) Stephan Tolksdorf 2008-2009
  2. // License: Simplified BSD License. See accompanying documentation.
  3. module FParsec.OperatorPrecedenceParser
  4. open FParsec.Primitives
  5. // Represents the associativity of infix operators.
  6. type Assoc = None = 0
  7. | Left = 1
  8. | Right = 2
  9. /// This union type is used to define operators for an OperatorPrecedenceParser.
  10. [<ReferenceEquality>]
  11. type PrecedenceParserOp<'a,'u> =
  12. // the operators are parsed as if they were given as "pstring opString >>? whitespaceAfterOpParser"
  13. /// PrefixOp(opString, wsAfterOpParser, precedence, isAssociative, f)
  14. /// represents a prefix operator definition for the `OperatorPrecedenceParser`.
  15. | PrefixOp of string * Parser<unit,'u> * int * bool * ('a -> 'a)
  16. /// PostfixOp(opString, wsAfterOpParser, precedence, isAssociative, f)
  17. /// represents a postfix operator definition for the `OperatorPrecedenceParser`.
  18. | PostfixOp of string * Parser<unit,'u> * int * bool * ('a -> 'a)
  19. /// InfixOp(opString, wsAfterOpParser, precedence, associativity, f)
  20. /// represents an infix operator definition for the `OperatorPrecedenceParser`.
  21. | InfixOp of string * Parser<unit,'u> * int * Assoc * ('a -> 'a -> 'a)
  22. /// TernaryOp(op1String, wsAfterOp1Parser, op2String, wsAfterOp2Parser, precedence, associativity, f)
  23. /// represents a ternary operator definition for the `OperatorPrecedenceParser`.
  24. | TernaryOp of string * Parser<unit,'u> *
  25. string * Parser<unit,'u> * int * Assoc * ('a -> 'a -> 'a -> 'a)
  26. /// PrefixOp'(opString, wsAfterOpParser, precedence, isAssociative, f),
  27. /// The state passed as an argument to the function is captured immediately before
  28. /// the operator string is parsed and contains the position of the operator.
  29. | PrefixOp' of string * Parser<unit,'u> * int * bool * (State<'u> -> 'a -> 'a)
  30. /// PostfixOp'(opString, wsAfterOpParser, precedence, isAssociative, f),
  31. /// The state passed as an argument to the function is captured immediately before
  32. /// the operator string is parsed and contains the position of the operator.
  33. | PostfixOp' of string * Parser<unit,'u> * int * bool * (State<'u> -> 'a -> 'a)
  34. /// InfixOp'(opString, wsAfterOpParser, precedence, associativity, f),
  35. /// The state passed as an argument to the function is captured immediately before
  36. /// the operator string is parsed and contains the position of the operator.
  37. | InfixOp' of string * Parser<unit,'u> * int * Assoc * (State<'u> -> 'a -> 'a -> 'a)
  38. /// TernaryOp'(op1String, wsAfterOp1Parser, op2String, wsAfterOp2Parser, precedence, associativity, f)
  39. /// The states passed as arguments to the function are captured immediately before
  40. /// the first and second operator strings are parsed and contain the positions of the operators.
  41. | TernaryOp' of string * Parser<unit,'u> *
  42. string * Parser<unit,'u> * int * Assoc * (State<'u> -> State<'u> -> 'a -> 'a -> 'a -> 'a)
  43. with
  44. override ToString: unit -> string
  45. /// Represents a dynamically configurable parser for parsing expressions involving
  46. /// prefix, postfix, infix and ternary operators of different precedence and associativity.
  47. [<Sealed>]
  48. type OperatorPrecedenceParser<'a,'u> =
  49. /// Constructs an OperatorPrecedenceParser instance and optionally adds the given operators.
  50. new: ?operators:seq<PrecedenceParserOp<'a,'u>> -> OperatorPrecedenceParser<'a,'u>
  51. // Operators with higher precedence bind tighter.
  52. // The middle expression in a ternary expression (e.g. expr2 in "expr1 ? expr2 : expr3")
  53. // is parsed as a "fresh" expression that is not influenced by the precedence of the
  54. // surrounding operators.
  55. // Expressions involving operators with identical precedence are parsed as follows
  56. // (where o1, o2 are two infix operators,
  57. // pre1, pre2 are two prefix operators,
  58. // po1, po2 are two postfix operators
  59. // and all operators have identical precedence):
  60. // x o1 pre1 y ==> x o1 (pre1 y),
  61. // x o1 y po1 ==> x o1 (y po1),
  62. // x o1 y o2 z ==> (x o1 y) o2 z, if o1 and o2 are left-associative
  63. // x o1 y o2 z ==> x o1 (y o2 z), if o1 and o2 are right-associative
  64. // pre1 x po1 ==> (pre1 x) po1, if pre1 or po1 is associative
  65. // pre1 pre2 x ==> pre1 (pre2 x), if pre1 or pre2 is associative
  66. // x po1 po2 ==> (x po1) po2, if po1 or po2 is associative
  67. // In the following situations the OperatorConflictHandler will be called
  68. // and the expression will only be parsed as indicated if the handler returns
  69. // an empty error message. The default handler always generate an error.
  70. // x o1 y o2 z ==> (x o1 y) o2 z, if o1 and o2 have different associativity,
  71. // or o1 and o2 are non-associative
  72. // pre1 pre2 x ==> pre1 (pre2 y), if pre1 and pre2 are non-associative
  73. // pre1 x po1 ==> (pre1 y) po1, if pre1 and po1 are non-associative
  74. // x po1 po2 ==> (y po1) po2, if po1 and po2 are non-associative
  75. /// The expression parser. This is a constant closure that forwards all work to an
  76. /// internal instance method, so that the expression parser always reflects the latest
  77. /// configuration of the `OperatorPrecedenceParser` instance.
  78. /// The parser can be safely called from different threads as long as the
  79. /// `OperatorPrecedenceParser` instance is not mutated at the same time.
  80. member ExpressionParser: Parser<'a,'u>
  81. /// This parser is called to parse the terms in between the operators. There is no default,
  82. /// so you must set this parser before you can call the `ExpressionParser`.
  83. /// Note that the term parser is also expected to parse any whitespace after a term.
  84. member TermParser: Parser<'a, 'u> with get, set
  85. /// This function is called when the precedence parser encounters two conflicting
  86. /// operators in the parser input.
  87. /// If the conflict handler returns `null` or an empty string, the operators are
  88. /// parsed as if both were (left-)associative, otherwise a parser error with the
  89. /// returned message is generated.
  90. /// The default handler will always generate an error message.
  91. member OperatorConflictHandler: ( State<'u> -> PrecedenceParserOp<'a,'u>
  92. -> State<'u> -> PrecedenceParserOp<'a,'u>
  93. -> string) with get, set
  94. /// Adds an operator to the grammar.
  95. /// Raises an `ArgumentException` if the operator definition conflicts with a
  96. /// a previous definition or contains empty strings or a non-positive precedence.
  97. member AddOperator: PrecedenceParserOp<'a,'u> -> unit
  98. /// Calls `AddOperator` with each operator in the given sequence.
  99. member AddOperators: seq<PrecedenceParserOp<'a,'u>> -> unit
  100. /// Removes the given operator from the grammar.
  101. /// Returns `false` if the operator was not previously registered, otherwise true.
  102. member RemoveOperator: PrecedenceParserOp<'a,'u> -> bool
  103. /// Removes the infix operator with the given string from the grammar.
  104. /// Returns `false` if no infix operator with that string was previously registered, otherwise `true`.
  105. member RemoveInfixOp: string -> bool
  106. /// Removes the prefix operator with the given string from the grammar.
  107. /// Returns `false` if no prefix operator with that string was previously registered, otherwise `true`.
  108. member RemovePrefixOp: string -> bool
  109. /// Removes the postfix operator with the given string from the grammar.
  110. /// Returns `false` if no postfix operator with that string was previously registered, otherwise `true`.
  111. member RemovePostfixOp: string -> bool
  112. /// Removes the ternary operator with the given strings from the grammar.
  113. /// Returns `false` if no ternary operator with these strings was previously registered, otherwise `true`.
  114. member RemoveTernaryOp: string*string -> bool
  115. /// Returns a sequence with a snapshot of the operators currently registered with the `OperatorPrecedenceParser`.
  116. member Operators: seq<PrecedenceParserOp<'a,'u>>