@@ -55,6 +55,7 @@ public enum Expr: Trackable, Hashable {
5555 case port( Port )
5656 indirect case tagged( Expr , Expr )
5757 case error( RuntimeError )
58+ indirect case syntax( SourcePosition , Expr )
5859
5960 /// Returns the type of this expression.
6061 public var type : Type {
@@ -117,6 +118,8 @@ public enum Expr: Trackable, Hashable {
117118 return . taggedType
118119 case . error( _) :
119120 return . errorType
121+ case . syntax( _, _) :
122+ return . syntaxType
120123 }
121124 }
122125
@@ -196,13 +199,28 @@ public enum Expr: Trackable, Hashable {
196199 }
197200 }
198201
202+ /// Returns the source position associated with this expression, or `outer` in case there is
203+ /// no syntax annotation.
204+ public func pos( _ outer: SourcePosition = SourcePosition . unknown) -> SourcePosition {
205+ switch self {
206+ case . syntax( let p, _) :
207+ return p
208+ case . pair( . syntax( let p, _) , _) :
209+ return p
210+ default :
211+ return outer
212+ }
213+ }
214+
199215 /// Returns the given expression with all symbols getting interned.
200216 public var datum : Expr {
201217 switch self {
202218 case . symbol( let sym) :
203219 return . symbol( sym. interned)
204220 case . pair( let car, let cdr) :
205221 return . pair( car. datum, cdr. datum)
222+ case . syntax( _, let expr) :
223+ return expr. datum
206224 default :
207225 return self
208226 }
@@ -247,6 +265,8 @@ public enum Expr: Trackable, Hashable {
247265 switch self {
248266 case . pair( let car, let cdr) :
249267 return car. requiresTracking || cdr. requiresTracking
268+ case . syntax( _, let expr) :
269+ return expr. requiresTracking
250270 case . box( _) , . mpair( _) , . vector( _) , . record( _) , . table( _) , . promise( _) ,
251271 . procedure( _) , . special( _) , . error( _) :
252272 return true
@@ -294,6 +314,8 @@ public enum Expr: Trackable, Hashable {
294314 case . error( let err) :
295315 err. mark ( tag)
296316 return
317+ case . syntax( _, let datum) :
318+ expr = datum
297319 default :
298320 return
299321 }
@@ -847,8 +869,11 @@ extension Expr: CustomStringConvertible {
847869 return " #<tag \( stringReprOf ( tag) ) : \( stringReprOf ( expr) ) > "
848870 case . error( let error) :
849871 return " #< \( error. inlineDescription) > "
872+ case . syntax( _, let expr) :
873+ return stringReprOf ( expr)
850874 }
851875 }
876+
852877 return stringReprOf ( self )
853878 }
854879
0 commit comments