Skip to content

Commit 556143c

Browse files
committed
commenting parser
1 parent 2b07f0f commit 556143c

File tree

4 files changed

+50
-4
lines changed

4 files changed

+50
-4
lines changed

src/libsyntax/parse/common.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,9 @@ pub impl Parser {
159159
}
160160
}
161161

162+
// if the given word is not a keyword, signal an error.
163+
// if the next token is the given keyword, eat it and return
164+
// true. Otherwise, return false.
162165
fn eat_keyword(&self, word: &~str) -> bool {
163166
self.require_keyword(word);
164167
let is_kw = match *self.token {
@@ -169,6 +172,9 @@ pub impl Parser {
169172
is_kw
170173
}
171174

175+
// if the given word is not a keyword, signal an error.
176+
// if the next token is not the given word, signal an error.
177+
// otherwise, eat it.
172178
fn expect_keyword(&self, word: &~str) {
173179
self.require_keyword(word);
174180
if !self.eat_keyword(word) {
@@ -182,10 +188,12 @@ pub impl Parser {
182188
}
183189
}
184190

191+
// return true if the given string is a strict keyword
185192
fn is_strict_keyword(&self, word: &~str) -> bool {
186193
self.strict_keywords.contains(word)
187194
}
188195

196+
// signal an error if the current token is a strict keyword
189197
fn check_strict_keywords(&self) {
190198
match *self.token {
191199
token::IDENT(_, false) => {
@@ -196,16 +204,19 @@ pub impl Parser {
196204
}
197205
}
198206

207+
// signal an error if the given string is a strict keyword
199208
fn check_strict_keywords_(&self, w: &~str) {
200209
if self.is_strict_keyword(w) {
201210
self.fatal(fmt!("found `%s` in ident position", *w));
202211
}
203212
}
204213

214+
// return true if this is a reserved keyword
205215
fn is_reserved_keyword(&self, word: &~str) -> bool {
206216
self.reserved_keywords.contains(word)
207217
}
208218

219+
// signal an error if the current token is a reserved keyword
209220
fn check_reserved_keywords(&self) {
210221
match *self.token {
211222
token::IDENT(_, false) => {
@@ -216,14 +227,16 @@ pub impl Parser {
216227
}
217228
}
218229

230+
// signal an error if the given string is a reserved keyword
219231
fn check_reserved_keywords_(&self, w: &~str) {
220232
if self.is_reserved_keyword(w) {
221233
self.fatal(fmt!("`%s` is a reserved keyword", *w));
222234
}
223235
}
224236

225237
// expect and consume a GT. if a >> is seen, replace it
226-
// with a single > and continue.
238+
// with a single > and continue. If a GT is not seen,
239+
// signal an error.
227240
fn expect_gt(&self) {
228241
if *self.token == token::GT {
229242
self.bump();

src/libsyntax/parse/lexer.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ pub fn new_low_level_string_reader(span_diagnostic: @span_handler,
8080
last_pos: filemap.start_pos,
8181
col: CharPos(0),
8282
curr: initial_char,
83-
filemap: filemap, interner: itr,
83+
filemap: filemap,
84+
interner: itr,
8485
/* dummy values; not read */
8586
peek_tok: token::EOF,
8687
peek_span: codemap::dummy_sp()
@@ -150,6 +151,7 @@ impl reader for TtReader {
150151
}
151152

152153
// EFFECT: advance peek_tok and peek_span to refer to the next token.
154+
// EFFECT: update the interner, maybe.
153155
fn string_advance_token(r: @mut StringReader) {
154156
match (consume_whitespace_and_comments(r)) {
155157
Some(comment) => {
@@ -539,6 +541,9 @@ fn ident_continue(c: char) -> bool {
539541
|| (c > 'z' && char::is_XID_continue(c))
540542
}
541543

544+
// return the next token from the string
545+
// EFFECT: advances the input past that token
546+
// EFFECT: updates the interner
542547
fn next_token_inner(rdr: @mut StringReader) -> token::Token {
543548
let mut accum_str = ~"";
544549
let mut c = rdr.curr;

src/libsyntax/parse/mod.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,14 @@ pub mod classify;
4545
/// Reporting obsolete syntax
4646
pub mod obsolete;
4747

48+
// info about a parsing session.
49+
// This structure and the reader both have
50+
// an interner associated with them. If they're
51+
// not the same, bad things can happen.
4852
pub struct ParseSess {
49-
cm: @codemap::CodeMap,
53+
cm: @codemap::CodeMap, // better be the same as the one in the reader!
5054
next_id: node_id,
51-
span_diagnostic: @span_handler,
55+
span_diagnostic: @span_handler, // better be the same as the one in the reader!
5256
interner: @ident_interner,
5357
}
5458

src/libsyntax/parse/parser.rs

+24
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ pub fn Parser(sess: @mut ParseSess,
246246
}
247247
}
248248

249+
// ooh, nasty mutable fields everywhere....
249250
pub struct Parser {
250251
sess: @mut ParseSess,
251252
cfg: crate_cfg,
@@ -338,13 +339,15 @@ pub impl Parser {
338339
self.sess.interner.get(id)
339340
}
340341

342+
// is this one of the keywords that signals a closure type?
341343
fn token_is_closure_keyword(&self, tok: &token::Token) -> bool {
342344
self.token_is_keyword(&~"pure", tok) ||
343345
self.token_is_keyword(&~"unsafe", tok) ||
344346
self.token_is_keyword(&~"once", tok) ||
345347
self.token_is_keyword(&~"fn", tok)
346348
}
347349

350+
// parse a ty_bare_fun type:
348351
fn parse_ty_bare_fn(&self) -> ty_
349352
{
350353
/*
@@ -372,6 +375,7 @@ pub impl Parser {
372375
});
373376
}
374377

378+
// parse a ty_closure type
375379
fn parse_ty_closure(&self,
376380
sigil: ast::Sigil,
377381
region: Option<@ast::Lifetime>) -> ty_
@@ -430,6 +434,7 @@ pub impl Parser {
430434
}
431435
}
432436

437+
// parse a function type (following the 'fn')
433438
fn parse_ty_fn_decl(&self) -> (fn_decl, OptVec<ast::Lifetime>) {
434439
/*
435440
@@ -541,12 +546,14 @@ pub impl Parser {
541546
}
542547

543548

549+
// parse a possibly mutable type
544550
fn parse_mt(&self) -> mt {
545551
let mutbl = self.parse_mutability();
546552
let t = self.parse_ty(false);
547553
mt { ty: t, mutbl: mutbl }
548554
}
549555

556+
// parse [mut/const/imm] ID : TY
550557
fn parse_ty_field(&self) -> ty_field {
551558
let lo = self.span.lo;
552559
let mutbl = self.parse_mutability();
@@ -563,6 +570,7 @@ pub impl Parser {
563570
)
564571
}
565572

573+
// parse optional return type [ -> TY ] in function decl
566574
fn parse_ret_ty(&self) -> (ret_style, @Ty) {
567575
return if self.eat(&token::RARROW) {
568576
let lo = self.span.lo;
@@ -591,6 +599,7 @@ pub impl Parser {
591599
}
592600
}
593601

602+
// parse a type.
594603
// Useless second parameter for compatibility with quasiquote macros.
595604
// Bleh!
596605
fn parse_ty(&self, _: bool) -> @Ty {
@@ -627,15 +636,19 @@ pub impl Parser {
627636
t
628637
}
629638
} else if *self.token == token::AT {
639+
// MANAGED POINTER
630640
self.bump();
631641
self.parse_box_or_uniq_pointee(ManagedSigil, ty_box)
632642
} else if *self.token == token::TILDE {
643+
// OWNED POINTER
633644
self.bump();
634645
self.parse_box_or_uniq_pointee(OwnedSigil, ty_uniq)
635646
} else if *self.token == token::BINOP(token::STAR) {
647+
// STAR POINTER (bare pointer?)
636648
self.bump();
637649
ty_ptr(self.parse_mt())
638650
} else if *self.token == token::LBRACE {
651+
// STRUCTURAL RECORD (remove?)
639652
let elems = self.parse_unspanned_seq(
640653
&token::LBRACE,
641654
&token::RBRACE,
@@ -648,6 +661,7 @@ pub impl Parser {
648661
self.obsolete(*self.last_span, ObsoleteRecordType);
649662
ty_nil
650663
} else if *self.token == token::LBRACKET {
664+
// VECTOR
651665
self.expect(&token::LBRACKET);
652666
let mt = self.parse_mt();
653667
if mt.mutbl == m_mutbl { // `m_const` too after snapshot
@@ -663,16 +677,20 @@ pub impl Parser {
663677
self.expect(&token::RBRACKET);
664678
t
665679
} else if *self.token == token::BINOP(token::AND) {
680+
// BORROWED POINTER
666681
self.bump();
667682
self.parse_borrowed_pointee()
668683
} else if self.eat_keyword(&~"extern") {
684+
// EXTERN FUNCTION
669685
self.parse_ty_bare_fn()
670686
} else if self.token_is_closure_keyword(&copy *self.token) {
687+
// CLOSURE
671688
let result = self.parse_ty_closure(ast::BorrowedSigil, None);
672689
self.obsolete(*self.last_span, ObsoleteBareFnType);
673690
result
674691
} else if *self.token == token::MOD_SEP
675692
|| is_ident_or_path(&*self.token) {
693+
// NAMED TYPE
676694
let path = self.parse_path_with_tps(false);
677695
ty_path(path, self.get_id())
678696
} else {
@@ -881,6 +899,8 @@ pub impl Parser {
881899
let global = self.eat(&token::MOD_SEP);
882900
let mut ids = ~[];
883901
loop {
902+
// if there's a ::< coming, stop processing
903+
// the path.
884904
let is_not_last =
885905
self.look_ahead(2u) != token::LT
886906
&& self.look_ahead(1u) == token::MOD_SEP;
@@ -900,6 +920,9 @@ pub impl Parser {
900920
types: ~[] }
901921
}
902922

923+
// parse a path optionally with type parameters. If 'colons'
924+
// is true, then type parameters must be preceded by colons,
925+
// as in a::t::<t1,t2>
903926
fn parse_path_with_tps(&self, colons: bool) -> @ast::path {
904927
debug!("parse_path_with_tps(colons=%b)", colons);
905928

@@ -1067,6 +1090,7 @@ pub impl Parser {
10671090
self.token_is_keyword(&~"const", tok)
10681091
}
10691092

1093+
// parse mutability declaration (mut/const/imm)
10701094
fn parse_mutability(&self) -> mutability {
10711095
if self.eat_keyword(&~"mut") {
10721096
m_mutbl

0 commit comments

Comments
 (0)