Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

More parsec work

  • Loading branch information...
commit cea4bd481685c9cdcf88ada1f2e1a8aed8d1303e 1 parent 69e7228
Karl Voelker authored May 07, 2012
5  parsec/parsec.rc
@@ -8,5 +8,8 @@
8 8
 #[license = "GPL"];
9 9
 #[crate_type = "lib"];
10 10
 
11  
-mod parsec;
  11
+mod parsec {
  12
+  mod core;
  13
+  mod extra;
  14
+}
12 15
 
79  parsec/parsec.rs → parsec/parsec/core.rs
@@ -46,7 +46,7 @@ fn eof<T>(s: state<T>) -> option<()> {
46 46
   }
47 47
 }
48 48
 
49  
-fn skip<T>(n: uint) -> parser<T, ()> {
  49
+fn skip_n<T>(n: uint) -> parser<T, ()> {
50 50
   ret fn@(s: state<T>) -> option<()> {
51 51
     if (s.pos + n > s.xs.len()) {
52 52
       s.error("Tried to skip past end of input")
@@ -57,7 +57,11 @@ fn skip<T>(n: uint) -> parser<T, ()> {
57 57
   };
58 58
 }
59 59
 
60  
-fn peek<T: copy>(n: uint) -> parser<T, [T]> {
  60
+fn skip<T>() -> parser<T, ()> {
  61
+  skip_n(1u)
  62
+}
  63
+
  64
+fn peek_n<T: copy>(n: uint) -> parser<T, [T]> {
61 65
   ret fn@(s: state<T>) -> option<[T]> {
62 66
     if (s.pos + n > s.xs.len()) {
63 67
       s.error("Tried to look past end of input")
@@ -67,34 +71,59 @@ fn peek<T: copy>(n: uint) -> parser<T, [T]> {
67 71
   };
68 72
 }
69 73
 
70  
-fn take<T: copy>(n: uint) -> parser<T, [T]> {
71  
-  peek(n).andThen(skip(n).pass())
  74
+fn peek<T: copy>() -> parser<T, T> {
  75
+  peek_n(1u).map({ |xs| vec::head(xs) })
72 76
 }
73 77
 
74  
-impl parser<T: copy, U: copy> for parser<T, U> {
  78
+fn take_n<T: copy>(n: uint) -> parser<T, [T]> {
  79
+  peek_n(n).andThen(pass(skip_n(n)))
  80
+}
75 81
 
76  
-  fn ignore() -> parser<T, ()> {
77  
-    ret fn@(s: state<T>) -> option<()> {
78  
-      s.apply(self);
79  
-      ret s.return(());
80  
-    };
  82
+fn take<T: copy>() -> parser<T, T> {
  83
+  take_n(1u).map({ |xs| vec::head(xs) })
  84
+}
  85
+
  86
+fn where<T: copy, U: copy>(p: fn@(U) -> bool) -> p_parser<U, T, U> {
  87
+  ret fn@(x: U) -> parser<T, U> {
  88
+    if (p(x)) {
  89
+      fn@(s: state<T>) -> option<U> { s.return(x) }
  90
+    } else {
  91
+      fn@(s: state<T>) -> option<U> { s.error("Predicate check failed") }
  92
+    }
  93
+  };
  94
+}
  95
+
  96
+fn ignore<T, U: copy>() -> p_parser<U, T, ()> {
  97
+  fn@(_x: U) -> parser<T, ()> {
  98
+    fn@(s: state<T>) -> option<()> {
  99
+      s.return(())
  100
+    }
81 101
   }
  102
+}
82 103
 
83  
-  fn pass<V: copy>() -> p_parser<V, T, V> {
84  
-    ret fn@(val: V) -> parser<T, V> {
85  
-      ret fn@(s: state<T>) -> option<V> {
86  
-        s.apply(self);
87  
-        ret s.return(val);
88  
-      };
89  
-    };
  104
+fn pass<T: copy, U: copy>(p: parser<T, ()>) -> p_parser<U, T, U> {
  105
+  fn@(val: U) -> parser<T, U> {
  106
+    fn@(s: state<T>) -> option<U> {
  107
+      p(s);
  108
+      s.return(val)
  109
+    }
90 110
   }
  111
+}
  112
+
  113
+impl parser<T: copy, U: copy> for parser<T, U> {
91 114
 
92 115
   fn andThen<V: copy>(next: p_parser<U, T, V>) -> parser<T, V> {
93  
-    ret fn@(s: state<T>) -> option<V> {
  116
+    fn@(s: state<T>) -> option<V> {
94 117
       s.apply(self).map_default(none, { |x|
95 118
         s.apply(next(x))
96 119
       })
97  
-    };
  120
+    }
  121
+  }
  122
+
  123
+  fn map<V: copy>(f: fn@(x: U) -> V) -> parser<T, V> {
  124
+    self.andThen(fn@(x: U) -> parser<T, V> {
  125
+      fn@(s: state<T>) -> option<V> { s.return(f(x)) }
  126
+    })
98 127
   }
99 128
 
100 129
   fn maybeTryOrElse<U: copy>(backtrack: bool, other: parser<T, U>) -> parser<T, U> {
@@ -121,7 +150,7 @@ impl parser<T: copy, U: copy> for parser<T, U> {
121 150
 
122 151
   fn tryParsePartial(xs: [T]) -> (state<T>, option<U>) {
123 152
     let s = state(@{xs: xs, mut pos: 0u, mut err: none});
124  
-    let r = self.andThen(bind eof(_).pass())(s);
  153
+    let r = self.andThen(pass(bind eof(_)))(s);
125 154
     ret (s, r);
126 155
   }
127 156
 
@@ -142,20 +171,13 @@ impl parser<T: copy, U: copy> for parser<T, U> {
142 171
 
143 172
 }
144 173
 
  174
+/*
145 175
 fn never_parser<T, U>(_s: state<T>) -> option<U> { none }
146 176
 
147 177
 fn never_p_parser<P, T, U: copy>(_val: P) -> parser<T, U> { bind never_parser(_) }
148 178
 
149 179
 impl option_parser<T: copy, U: copy> for option<parser<T, U>> {
150 180
 
151  
-  fn ignore() -> parser<T, ()> {
152  
-    self.map_default(bind never_parser(_), { |p| p.ignore() })
153  
-  }
154  
-
155  
-  fn pass<V: copy>() -> p_parser<V, T, V> {
156  
-    self.map_default(bind never_p_parser(_), { |p| p.pass() })
157  
-  }
158  
-
159 181
   fn andThen<V: copy>(next: p_parser<U, T, V>) -> parser<T, V> {
160 182
     self.map_default(bind never_parser(_), { |p| p.andThen(next) })
161 183
   }
@@ -165,4 +187,5 @@ impl option_parser<T: copy, U: copy> for option<parser<T, U>> {
165 187
   }
166 188
 
167 189
 }
  190
+*/
168 191
 
7  parsec/parsec/extra.rs
... ...
@@ -0,0 +1,7 @@
  1
+
  2
+import core::*;
  3
+
  4
+fn takeWhere<T: copy>(p: fn@(T) -> bool) -> parser<T, T> {
  5
+  peek().andThen(where(p)).andThen(pass(skip()))
  6
+}
  7
+

0 notes on commit cea4bd4

Please sign in to comment.
Something went wrong with that request. Please try again.