feat(library/init/lean/elaborator): initial version of translating syntax trees to preterms
diff --git a/library/init/lean/elaborator.lean b/library/init/lean/elaborator.lean
index 1efdc4f..44d3040 100644
--- a/library/init/lean/elaborator.lean
+++ b/library/init/lean/elaborator.lean
@@ -7,6 +7,8 @@
 -/
 prelude
 import init.lean.parser.module
+import init.lean.expander
+import init.lean.expr
 
 namespace lean
 namespace elaborator
@@ -62,6 +64,69 @@
 
 local attribute [instance] name.has_lt_quick
 
+def mangle_ident (v : ident.view) : name :=
+v.scopes.foldl name.mk_numeral v.to_name
+
+def to_level : syntax → elaborator_m level
+| stx := do
+  some k ← pure stx.kind | error stx $ "unexpected atom: " ++ to_string stx,
+  match k with
+  | level.leading := (match view level.leading stx with
+    | level.leading.view.hole _ := pure $ level.mvar "u"  -- TODO(Sebastian): name?
+    | level.leading.view.lit lit := pure $ level.of_nat lit.to_nat
+    | level.leading.view.var id := pure $ level.param $ mangle_ident id
+    | _ := error stx "ill-formed universe level")
+  | _ := error stx $ "unexpected node: " ++ to_string k.name
+
+def to_pexpr : syntax → elaborator_m expr
+| stx := do
+  some k ← pure stx.kind | error stx $ "unexpected atom: " ++ to_string stx,
+  match k with
+  | @ident_univs := (match view ident_univs stx with
+    | {id := id, univs := some univs} := expr.const (mangle_ident id) <$> univs.levels.mmap to_level
+    | {id := id, univs := none}       := pure $ expr.const (mangle_ident id) [])
+  | @app   := let v := view app stx in
+    expr.app <$> to_pexpr v.fn <*> to_pexpr v.arg
+  | @lambda := do
+    ({binders := {leading_ids := [],
+                  remainder := binders_remainder.view.mixed [mixed_binder.view.bracketed bb]},
+      body := body}) ←
+      pure $ view lambda stx | error stx "ill-formed lambda",
+    (bi, bc) ← match bb with
+    | bracketed_binder.view.explicit {content := bc} :=
+      (match bc with
+       | explicit_binder_content.view.other bc := pure (binder_info.default, bc)
+       | _ := error stx "ill-formed lambda")
+    | bracketed_binder.view.implicit {content := bc} :=
+      pure (binder_info.implicit, bc)
+    | bracketed_binder.view.strict_implicit {content := bc} :=
+      pure (binder_info.strict_implicit, bc)
+    | bracketed_binder.view.inst_implicit {content := bc} :=
+      prod.mk binder_info.inst_implicit <$> (match bc.kind with
+        | some @inst_implicit_anonymous_binder :=
+          pure {ids := ["_inst_"], type := some {type := (view inst_implicit_anonymous_binder bc).type}}
+        | some @inst_implicit_named_binder :=
+          let v := view inst_implicit_named_binder bc in
+          pure {ids := [v.id], type := some {type := v.type}}
+        | _ := error stx "ill-formed lambda")
+    /-| bracketed_binder.view.anonymous_constructor {content := bc} :=-/
+    | _ := error stx "ill-formed lambda",
+    {ids := [binder_ident.view.id id], type := some type} ← pure bc | error stx "ill-formed lambda",
+    expr.lam (mangle_ident id) bi <$> to_pexpr type.type <*> to_pexpr body
+  | @sort := (match view sort stx with
+    | sort.view.Sort _ := pure $ expr.sort level.zero
+    | sort.view.Type _ := pure $ expr.sort $ level.succ level.zero)
+  | _ := error stx $ "unexpected node: " ++ to_string k.name
+
+def declaration.elaborate : elaborator :=
+λ stx, do
+  let decl := view «declaration» stx,
+  -- just test `to_pexpr` for now
+  match decl.inner with
+  | declaration.inner.view.def_like {val := decl_val.view.simple val, ..} :=
+    to_pexpr val.body *> pure ()
+  | _ := pure ()
+
 def prec_to_nat : option precedence.view → nat
 | (some prec) := prec.term.to_nat
 | none        := 0
@@ -284,7 +349,8 @@
   (notation.name, notation.elaborate),
   (reserve_notation.name, reserve_notation.elaborate),
   (section.name, section.elaborate),
-  (namespace.name, namespace.elaborate)
+  (namespace.name, namespace.elaborate),
+  (declaration.name, declaration.elaborate)
 ] _
 
 protected def max_recursion := 100
diff --git a/library/init/lean/parser/syntax.lean b/library/init/lean/parser/syntax.lean
index 486b7b9..f5ead48 100644
--- a/library/init/lean/parser/syntax.lean
+++ b/library/init/lean/parser/syntax.lean
@@ -40,7 +40,7 @@
 @[pattern] def no_kind : syntax_node_kind := ⟨`lean.parser.no_kind⟩
 
 /-- A hygiene marker introduced by a macro expansion. -/
-@[irreducible, derive decidable_eq has_to_format]
+@[derive decidable_eq has_to_format]
 def macro_scope := nat
 
 /-
diff --git a/library/init/lean/parser/term.lean b/library/init/lean/parser/term.lean
index 30789d6..dde944a 100644
--- a/library/init/lean/parser/term.lean
+++ b/library/init/lean/parser/term.lean
@@ -24,7 +24,7 @@
 
 @[derive parser.has_tokens parser.has_view]
 def ident_univs.parser : term_parser :=
-node! ident_univs [id: ident.parser, univ: (monad_lift ident_univ_spec.parser)?]
+node! ident_univs [id: ident.parser, univs: (monad_lift ident_univ_spec.parser)?]
 
 namespace term
 /-- Access leading term -/
diff --git a/library/init/lean/parser/token.lean b/library/init/lean/parser/token.lean
index 268c6e7..6111e16 100644
--- a/library/init/lean/parser/token.lean
+++ b/library/init/lean/parser/token.lean
@@ -135,11 +135,11 @@
 try (lookahead (ch '.' *> (ch id_begin_escape <|> satisfy is_id_first)))
 *> node! ident_suffix [«.»: raw_str ".", ident: recurse ()]
 
-def ident'' : rec_t unit syntax basic_parser_m syntax :=
-node! ident [part: monad_lift ident_part.parser, suffix: optional ident_suffix.parser]
+def ident''.parser : rec_t unit syntax basic_parser_m syntax :=
+node! ident'' [part: monad_lift ident_part.parser, suffix: optional ident_suffix.parser]
 
 private def ident' : basic_parser_m syntax :=
-rec_t.run_parsec ident'' $ λ _, ident''
+rec_t.run_parsec ident''.parser $ λ _, ident''.parser
 
 --TODO(Sebastian): other bases
 def number' : basic_parser :=
@@ -255,6 +255,9 @@
 instance string_lit.parser.view : parser.has_view string_lit.view (string_lit.parser : parser) :=
 {..string_lit.has_view}
 
+-- redefine with extended view: ident is the only syntax node where we want to access the macro scopes
+@[pattern] def ident := ident''
+
 def ident.parser : parser :=
 lift $ try $ do {
   it ← left_over,
@@ -263,6 +266,14 @@
   else error "" (dlist.singleton "identifier") it
 } <?> "identifier"
 
+structure ident.view extends ident''.view :=
+(scopes : list macro_scope := [])
+
+-- extend and shadow previous instance
+instance ident.has_view : parser.has_view ident.view ident :=
+{ view := λ stx, {view ident'' stx with scopes := (syntax_node.scopes <$> stx.as_node).get_or_else []},
+  review := λ v, (review ident'' v.to_view).flip_scopes v.scopes }
+
 instance ident.parser.tokens : parser.has_tokens (ident.parser : parser) := default _
 instance ident.parser.view : parser.has_view ident.view (ident.parser : parser) :=
 {..ident.has_view}