Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 part of scanner; | 5 part of scanner; |
| 6 | 6 |
| 7 /** | 7 /** |
| 8 * An event generating parser of Dart programs. This parser expects | 8 * An event generating parser of Dart programs. This parser expects |
| 9 * all tokens in a linked list (aka a token stream). | 9 * all tokens in a linked list (aka a token stream). |
| 10 * | 10 * |
| (...skipping 345 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 356 token = parseFormalParameters(token); | 356 token = parseFormalParameters(token); |
| 357 listener.handleFunctionTypedFormalParameter(token); | 357 listener.handleFunctionTypedFormalParameter(token); |
| 358 } | 358 } |
| 359 String value = token.stringValue; | 359 String value = token.stringValue; |
| 360 if ((identical('=', value)) || (identical(':', value))) { | 360 if ((identical('=', value)) || (identical(':', value))) { |
| 361 // TODO(ahe): Validate that these are only used for optional parameters. | 361 // TODO(ahe): Validate that these are only used for optional parameters. |
| 362 Token equal = token; | 362 Token equal = token; |
| 363 token = parseExpression(token.next); | 363 token = parseExpression(token.next); |
| 364 listener.handleValuedFormalParameter(equal, token); | 364 listener.handleValuedFormalParameter(equal, token); |
| 365 } | 365 } |
| 366 listener.endFormalParameter(token, thisKeyword); | 366 listener.endFormalParameter(thisKeyword); |
| 367 return token; | 367 return token; |
| 368 } | 368 } |
| 369 | 369 |
| 370 Token parseOptionalFormalParameters(Token token, bool isNamed) { | 370 Token parseOptionalFormalParameters(Token token, bool isNamed) { |
| 371 Token begin = token; | 371 Token begin = token; |
| 372 listener.beginOptionalFormalParameters(begin); | 372 listener.beginOptionalFormalParameters(begin); |
| 373 assert((isNamed && optional('{', token)) || optional('[', token)); | 373 assert((isNamed && optional('{', token)) || optional('[', token)); |
| 374 int parameterCount = 0; | 374 int parameterCount = 0; |
| 375 do { | 375 do { |
| 376 token = token.next; | 376 token = token.next; |
| (...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 547 } else { | 547 } else { |
| 548 listener.handleNoType(token); | 548 listener.handleNoType(token); |
| 549 } | 549 } |
| 550 listener.endTypeVariable(token); | 550 listener.endTypeVariable(token); |
| 551 return token; | 551 return token; |
| 552 } | 552 } |
| 553 | 553 |
| 554 /** | 554 /** |
| 555 * Returns true if the stringValue of the [token] is [value]. | 555 * Returns true if the stringValue of the [token] is [value]. |
| 556 */ | 556 */ |
| 557 bool optional(String value, Token token) => identical(value, token.stringValue ); | 557 bool optional(String value, Token token) => |
| 558 identical(value, token.stringValue); | |
|
ahe
2013/02/04 13:38:26
Since it doesn't fit on one line, I'd prefer using
Johnni Winther
2013/02/05 09:03:09
Done.
| |
| 559 | |
| 560 /** | |
| 561 * Returns true if the stringValue of the [token] is either [value1], | |
| 562 * [value2], [value3], or [value4]. | |
| 563 */ | |
| 564 bool isOneOf4(Token token, | |
| 565 String value1, String value2, String value3, String value4) { | |
| 566 String stringValue = token.stringValue; | |
| 567 return identical(value1, stringValue) || | |
| 568 identical(value2, stringValue) || | |
| 569 identical(value3, stringValue) || | |
| 570 identical(value4, stringValue); | |
| 571 } | |
| 558 | 572 |
| 559 bool notEofOrValue(String value, Token token) { | 573 bool notEofOrValue(String value, Token token) { |
| 560 return !identical(token.kind, EOF_TOKEN) && !identical(value, token.stringVa lue); | 574 return !identical(token.kind, EOF_TOKEN) && |
| 575 !identical(value, token.stringValue); | |
| 561 } | 576 } |
| 562 | 577 |
| 563 Token parseType(Token token) { | 578 Token parseType(Token token) { |
| 564 Token begin = token; | 579 Token begin = token; |
| 565 if (isValidTypeReference(token)) { | 580 if (isValidTypeReference(token)) { |
| 566 token = parseIdentifier(token); | 581 token = parseIdentifier(token); |
| 567 token = parseQualifiedRestOpt(token); | 582 token = parseQualifiedRestOpt(token); |
| 568 } else { | 583 } else { |
| 569 token = listener.expectedType(token); | 584 token = listener.expectedType(token); |
| 570 } | 585 } |
| (...skipping 1290 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1861 listener.handleAsOperator(operator, token); | 1876 listener.handleAsOperator(operator, token); |
| 1862 String value = token.stringValue; | 1877 String value = token.stringValue; |
| 1863 if (identical(value, 'is') || identical(value, 'as')) { | 1878 if (identical(value, 'is') || identical(value, 'as')) { |
| 1864 // The is- and as-operators cannot be chained. | 1879 // The is- and as-operators cannot be chained. |
| 1865 listener.unexpected(token); | 1880 listener.unexpected(token); |
| 1866 } | 1881 } |
| 1867 return token; | 1882 return token; |
| 1868 } | 1883 } |
| 1869 | 1884 |
| 1870 Token parseVariablesDeclaration(Token token) { | 1885 Token parseVariablesDeclaration(Token token) { |
| 1871 token = parseVariablesDeclarationNoSemicolon(token); | 1886 return parseVariablesDeclarationMaybeSemicolon(token, true); |
| 1872 return expectSemicolon(token); | |
| 1873 } | 1887 } |
| 1874 | 1888 |
| 1875 Token parseVariablesDeclarationNoSemicolon(Token token) { | 1889 Token parseVariablesDeclarationNoSemicolon(Token token) { |
| 1890 return parseVariablesDeclarationMaybeSemicolon(token, false); | |
| 1891 } | |
| 1892 | |
| 1893 Token parseVariablesDeclarationMaybeSemicolon(Token token, | |
| 1894 bool endWithSemicolon) { | |
| 1876 int count = 1; | 1895 int count = 1; |
| 1877 listener.beginVariablesDeclaration(token); | 1896 listener.beginVariablesDeclaration(token); |
| 1878 token = parseModifiers(token); | 1897 token = parseModifiers(token); |
| 1879 token = parseTypeOpt(token); | 1898 token = parseTypeOpt(token); |
| 1880 token = parseOptionallyInitializedIdentifier(token); | 1899 token = parseOptionallyInitializedIdentifier(token); |
| 1881 while (optional(',', token)) { | 1900 while (optional(',', token)) { |
| 1882 token = parseOptionallyInitializedIdentifier(token.next); | 1901 token = parseOptionallyInitializedIdentifier(token.next); |
| 1883 ++count; | 1902 ++count; |
| 1884 } | 1903 } |
| 1885 listener.endVariablesDeclaration(count, token); | 1904 if (endWithSemicolon) { |
| 1886 return token; | 1905 expectSemicolon(token); |
|
ahe
2013/02/04 13:38:26
I have a prototype that recovers on missing semico
Johnni Winther
2013/02/05 09:03:09
Done.
| |
| 1906 listener.endVariablesDeclaration(count, token); | |
| 1907 return token.next; | |
| 1908 } else { | |
| 1909 listener.endVariablesDeclaration(count, null); | |
| 1910 return token; | |
| 1911 } | |
| 1887 } | 1912 } |
| 1888 | 1913 |
| 1889 Token parseOptionallyInitializedIdentifier(Token token) { | 1914 Token parseOptionallyInitializedIdentifier(Token token) { |
| 1890 listener.beginInitializedIdentifier(token); | 1915 listener.beginInitializedIdentifier(token); |
| 1891 token = parseIdentifier(token); | 1916 token = parseIdentifier(token); |
| 1892 token = parseVariableInitializerOpt(token); | 1917 token = parseVariableInitializerOpt(token); |
| 1893 listener.endInitializedIdentifier(); | 1918 listener.endInitializedIdentifier(); |
| 1894 return token; | 1919 return token; |
| 1895 } | 1920 } |
| 1896 | 1921 |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 1926 final String value = token.stringValue; | 1951 final String value = token.stringValue; |
| 1927 if (identical(value, ';')) { | 1952 if (identical(value, ';')) { |
| 1928 listener.handleNoExpression(token); | 1953 listener.handleNoExpression(token); |
| 1929 return token; | 1954 return token; |
| 1930 } else if ((identical(value, 'var')) || (identical(value, 'final'))) { | 1955 } else if ((identical(value, 'var')) || (identical(value, 'final'))) { |
| 1931 return parseVariablesDeclarationNoSemicolon(token); | 1956 return parseVariablesDeclarationNoSemicolon(token); |
| 1932 } | 1957 } |
| 1933 Token identifier = peekIdentifierAfterType(token); | 1958 Token identifier = peekIdentifierAfterType(token); |
| 1934 if (identifier != null) { | 1959 if (identifier != null) { |
| 1935 assert(identifier.isIdentifier()); | 1960 assert(identifier.isIdentifier()); |
| 1936 Token afterId = identifier.next; | 1961 if (isOneOf4(identifier.next, '=', ';', ',', 'in')) { |
| 1937 int afterIdKind = afterId.kind; | |
| 1938 if (identical(afterIdKind, EQ_TOKEN) || identical(afterIdKind, SEMICOLON_T OKEN) || | |
| 1939 identical(afterIdKind, COMMA_TOKEN) || optional('in', afterId)) { | |
| 1940 return parseVariablesDeclarationNoSemicolon(token); | 1962 return parseVariablesDeclarationNoSemicolon(token); |
| 1941 } | 1963 } |
| 1942 } | 1964 } |
| 1943 return parseExpression(token); | 1965 return parseExpression(token); |
| 1944 } | 1966 } |
| 1945 | 1967 |
| 1946 Token parseForRest(Token forToken, Token token) { | 1968 Token parseForRest(Token forToken, Token token) { |
| 1947 token = expectSemicolon(token); | 1969 token = expectSemicolon(token); |
| 1948 if (optional(';', token)) { | 1970 if (optional(';', token)) { |
| 1949 token = parseEmptyStatement(token); | 1971 token = parseEmptyStatement(token); |
| (...skipping 247 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2197 } | 2219 } |
| 2198 listener.handleContinueStatement(hasTarget, continueKeyword, token); | 2220 listener.handleContinueStatement(hasTarget, continueKeyword, token); |
| 2199 return expectSemicolon(token); | 2221 return expectSemicolon(token); |
| 2200 } | 2222 } |
| 2201 | 2223 |
| 2202 Token parseEmptyStatement(Token token) { | 2224 Token parseEmptyStatement(Token token) { |
| 2203 listener.handleEmptyStatement(token); | 2225 listener.handleEmptyStatement(token); |
| 2204 return expectSemicolon(token); | 2226 return expectSemicolon(token); |
| 2205 } | 2227 } |
| 2206 } | 2228 } |
| OLD | NEW |