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 library fasta.parser.parser; | 5 library fasta.parser.parser; |
| 6 | 6 |
| 7 import '../scanner.dart' show ErrorToken; | 7 import '../scanner.dart' show ErrorToken; |
| 8 | 8 |
| 9 import '../scanner/recover.dart' show closeBraceFor, skipToEof; | 9 import '../scanner/recover.dart' show closeBraceFor, skipToEof; |
| 10 | 10 |
| (...skipping 501 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 512 Token covariantKeyword; | 512 Token covariantKeyword; |
| 513 if (identical(token.stringValue, 'covariant') && | 513 if (identical(token.stringValue, 'covariant') && |
| 514 (token.next.isIdentifier() || isModifier(token.next))) { | 514 (token.next.isIdentifier() || isModifier(token.next))) { |
| 515 covariantKeyword = token; | 515 covariantKeyword = token; |
| 516 token = token.next; | 516 token = token.next; |
| 517 } | 517 } |
| 518 token = parseModifiers(token); | 518 token = parseModifiers(token); |
| 519 bool isNamedParameter = kind == FormalParameterType.NAMED; | 519 bool isNamedParameter = kind == FormalParameterType.NAMED; |
| 520 | 520 |
| 521 Token thisKeyword = null; | 521 Token thisKeyword = null; |
| 522 Token nameToken; | |
| 522 if (inFunctionType && isNamedParameter) { | 523 if (inFunctionType && isNamedParameter) { |
| 523 token = parseType(token); | 524 token = parseType(token); |
| 524 token = | 525 token = |
| 525 parseIdentifier(token, IdentifierContext.formalParameterDeclaration); | 526 parseIdentifier(token, IdentifierContext.formalParameterDeclaration); |
| 526 } else if (inFunctionType) { | 527 } else if (inFunctionType) { |
| 527 token = parseType(token); | 528 token = parseType(token); |
| 528 if (token.isIdentifier()) { | 529 if (token.isIdentifier()) { |
| 529 token = parseIdentifier( | 530 token = parseIdentifier( |
| 530 token, IdentifierContext.formalParameterDeclaration); | 531 token, IdentifierContext.formalParameterDeclaration); |
| 531 } else { | 532 } else { |
| 532 listener.handleNoName(token); | 533 listener.handleNoName(token); |
| 533 } | 534 } |
| 534 } else { | 535 } else { |
| 535 token = parseReturnTypeOpt(token); | 536 token = parseReturnTypeOpt(token); |
| 536 if (optional('this', token)) { | 537 if (optional('this', token)) { |
| 537 thisKeyword = token; | 538 thisKeyword = token; |
| 538 token = expect('.', token.next); | 539 token = expect('.', token.next); |
| 540 nameToken = token; | |
| 539 token = parseIdentifier(token, IdentifierContext.fieldInitializer); | 541 token = parseIdentifier(token, IdentifierContext.fieldInitializer); |
| 540 } else { | 542 } else { |
| 543 nameToken = token; | |
| 541 token = parseIdentifier( | 544 token = parseIdentifier( |
| 542 token, IdentifierContext.formalParameterDeclaration); | 545 token, IdentifierContext.formalParameterDeclaration); |
| 543 } | 546 } |
| 544 } | 547 } |
| 545 | 548 |
| 546 if (optional('(', token)) { | 549 if (optional('(', token)) { |
| 547 Token inlineFunctionTypeStart = token; | 550 Token inlineFunctionTypeStart = token; |
| 548 listener.beginFunctionTypedFormalParameter(token); | 551 listener.beginFunctionTypedFormalParameter(token); |
| 549 listener.handleNoTypeVariables(token); | 552 listener.handleNoTypeVariables(token); |
| 550 token = parseFormalParameters(token); | 553 token = parseFormalParameters(token); |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 579 token = parseExpression(token.next); | 582 token = parseExpression(token.next); |
| 580 listener.handleValuedFormalParameter(equal, token); | 583 listener.handleValuedFormalParameter(equal, token); |
| 581 if (kind.isRequired) { | 584 if (kind.isRequired) { |
| 582 reportRecoverableError(equal, ErrorKind.RequiredParameterWithDefault); | 585 reportRecoverableError(equal, ErrorKind.RequiredParameterWithDefault); |
| 583 } else if (kind.isPositional && identical(':', value)) { | 586 } else if (kind.isPositional && identical(':', value)) { |
| 584 reportRecoverableError(equal, ErrorKind.PositionalParameterWithEquals); | 587 reportRecoverableError(equal, ErrorKind.PositionalParameterWithEquals); |
| 585 } | 588 } |
| 586 } else { | 589 } else { |
| 587 listener.handleFormalParameterWithoutValue(token); | 590 listener.handleFormalParameterWithoutValue(token); |
| 588 } | 591 } |
| 589 listener.endFormalParameter(covariantKeyword, thisKeyword, kind); | 592 listener.endFormalParameter(covariantKeyword, thisKeyword, nameToken, kind); |
| 590 return token; | 593 return token; |
| 591 } | 594 } |
| 592 | 595 |
| 593 Token parseOptionalFormalParameters(Token token, bool isNamed, | 596 Token parseOptionalFormalParameters(Token token, bool isNamed, |
| 594 {bool inFunctionType: false}) { | 597 {bool inFunctionType: false}) { |
| 595 Token begin = token; | 598 Token begin = token; |
| 596 listener.beginOptionalFormalParameters(begin); | 599 listener.beginOptionalFormalParameters(begin); |
| 597 assert((isNamed && optional('{', token)) || optional('[', token)); | 600 assert((isNamed && optional('{', token)) || optional('[', token)); |
| 598 int parameterCount = 0; | 601 int parameterCount = 0; |
| 599 do { | 602 do { |
| (...skipping 672 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1272 parseReturnTypeOpt(type); | 1275 parseReturnTypeOpt(type); |
| 1273 } | 1276 } |
| 1274 Token token = | 1277 Token token = |
| 1275 parseIdentifier(name, IdentifierContext.topLevelFunctionDeclaration); | 1278 parseIdentifier(name, IdentifierContext.topLevelFunctionDeclaration); |
| 1276 | 1279 |
| 1277 if (getOrSet == null) { | 1280 if (getOrSet == null) { |
| 1278 token = parseTypeVariablesOpt(token); | 1281 token = parseTypeVariablesOpt(token); |
| 1279 } else { | 1282 } else { |
| 1280 listener.handleNoTypeVariables(token); | 1283 listener.handleNoTypeVariables(token); |
| 1281 } | 1284 } |
| 1285 Token formalParametersToken = token; | |
| 1282 token = parseFormalParametersOpt(token); | 1286 token = parseFormalParametersOpt(token); |
| 1283 bool previousAsyncAwaitKeywordsEnabled = asyncAwaitKeywordsEnabled; | 1287 bool previousAsyncAwaitKeywordsEnabled = asyncAwaitKeywordsEnabled; |
| 1284 token = parseAsyncModifier(token); | 1288 token = parseAsyncModifier(token); |
| 1285 token = parseFunctionBody(token, false, externalModifier != null); | 1289 token = parseFunctionBody(token, false, externalModifier != null); |
| 1286 asyncAwaitKeywordsEnabled = previousAsyncAwaitKeywordsEnabled; | 1290 asyncAwaitKeywordsEnabled = previousAsyncAwaitKeywordsEnabled; |
| 1287 Token endToken = token; | 1291 Token endToken = token; |
| 1288 token = token.next; | 1292 token = token.next; |
| 1289 listener.endTopLevelMethod(start, getOrSet, endToken); | 1293 listener.endTopLevelMethod( |
| 1294 start, getOrSet, formalParametersToken, endToken); | |
|
ahe
2017/03/13 14:33:52
Have you tested this situation:
get foo => "foo";
jensj
2017/03/14 12:37:12
Well -- the name is misleading (the token is '=>')
| |
| 1290 return token; | 1295 return token; |
| 1291 } | 1296 } |
| 1292 | 1297 |
| 1293 /// Looks ahead to find the name of a member. Returns a link of the modifiers, | 1298 /// Looks ahead to find the name of a member. Returns a link of the modifiers, |
| 1294 /// set/get, (operator) name, and either the start of the method body or the | 1299 /// set/get, (operator) name, and either the start of the method body or the |
| 1295 /// end of the declaration. | 1300 /// end of the declaration. |
| 1296 /// | 1301 /// |
| 1297 /// Examples: | 1302 /// Examples: |
| 1298 /// | 1303 /// |
| 1299 /// int get foo; | 1304 /// int get foo; |
| (...skipping 512 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1812 token = parseIdentifier(name, IdentifierContext.methodDeclaration); | 1817 token = parseIdentifier(name, IdentifierContext.methodDeclaration); |
| 1813 } | 1818 } |
| 1814 | 1819 |
| 1815 token = parseQualifiedRestOpt( | 1820 token = parseQualifiedRestOpt( |
| 1816 token, IdentifierContext.methodDeclarationContinuation); | 1821 token, IdentifierContext.methodDeclarationContinuation); |
| 1817 if (getOrSet == null) { | 1822 if (getOrSet == null) { |
| 1818 token = parseTypeVariablesOpt(token); | 1823 token = parseTypeVariablesOpt(token); |
| 1819 } else { | 1824 } else { |
| 1820 listener.handleNoTypeVariables(token); | 1825 listener.handleNoTypeVariables(token); |
| 1821 } | 1826 } |
| 1827 Token formalParametersToken = token; | |
| 1822 token = parseFormalParametersOpt(token); | 1828 token = parseFormalParametersOpt(token); |
| 1823 token = parseInitializersOpt(token); | 1829 token = parseInitializersOpt(token); |
| 1824 bool previousAsyncAwaitKeywordsEnabled = asyncAwaitKeywordsEnabled; | 1830 bool previousAsyncAwaitKeywordsEnabled = asyncAwaitKeywordsEnabled; |
| 1825 token = parseAsyncModifier(token); | 1831 token = parseAsyncModifier(token); |
| 1826 if (optional('=', token)) { | 1832 if (optional('=', token)) { |
| 1827 token = parseRedirectingFactoryBody(token); | 1833 token = parseRedirectingFactoryBody(token); |
| 1828 } else { | 1834 } else { |
| 1829 token = parseFunctionBody( | 1835 token = parseFunctionBody( |
| 1830 token, false, staticModifier == null || externalModifier != null); | 1836 token, false, staticModifier == null || externalModifier != null); |
| 1831 } | 1837 } |
| 1832 asyncAwaitKeywordsEnabled = previousAsyncAwaitKeywordsEnabled; | 1838 asyncAwaitKeywordsEnabled = previousAsyncAwaitKeywordsEnabled; |
| 1833 listener.endMethod(getOrSet, start, token); | 1839 listener.endMethod(getOrSet, start, formalParametersToken, token); |
| 1834 return token.next; | 1840 return token.next; |
| 1835 } | 1841 } |
| 1836 | 1842 |
| 1837 Token parseFactoryMethod(Token token) { | 1843 Token parseFactoryMethod(Token token) { |
| 1838 assert(isFactoryDeclaration(token)); | 1844 assert(isFactoryDeclaration(token)); |
| 1839 Token start = token; | 1845 Token start = token; |
| 1840 bool isExternal = false; | 1846 bool isExternal = false; |
| 1841 int modifierCount = 0; | 1847 int modifierCount = 0; |
| 1842 while (isModifier(token)) { | 1848 while (isModifier(token)) { |
| 1843 if (optional('external', token)) { | 1849 if (optional('external', token)) { |
| 1844 isExternal = true; | 1850 isExternal = true; |
| 1845 } | 1851 } |
| 1846 token = parseModifier(token); | 1852 token = parseModifier(token); |
| 1847 modifierCount++; | 1853 modifierCount++; |
| 1848 } | 1854 } |
| 1849 listener.handleModifiers(modifierCount); | 1855 listener.handleModifiers(modifierCount); |
| 1850 Token factoryKeyword = token; | 1856 Token factoryKeyword = token; |
| 1851 listener.beginFactoryMethod(factoryKeyword); | 1857 listener.beginFactoryMethod(factoryKeyword); |
| 1852 token = expect('factory', token); | 1858 token = expect('factory', token); |
| 1853 token = parseConstructorReference(token); | 1859 token = parseConstructorReference(token); |
| 1860 Token formalParametersToken = token; | |
| 1854 token = parseFormalParameters(token); | 1861 token = parseFormalParameters(token); |
| 1855 token = parseAsyncModifier(token); | 1862 token = parseAsyncModifier(token); |
| 1856 if (optional('=', token)) { | 1863 if (optional('=', token)) { |
| 1857 token = parseRedirectingFactoryBody(token); | 1864 token = parseRedirectingFactoryBody(token); |
| 1858 } else { | 1865 } else { |
| 1859 token = parseFunctionBody(token, false, isExternal); | 1866 token = parseFunctionBody(token, false, isExternal); |
| 1860 } | 1867 } |
| 1861 listener.endFactoryMethod(start, factoryKeyword, token); | 1868 listener.endFactoryMethod( |
| 1869 start, factoryKeyword, formalParametersToken, token); | |
| 1862 return token.next; | 1870 return token.next; |
| 1863 } | 1871 } |
| 1864 | 1872 |
| 1865 Token parseOperatorName(Token token) { | 1873 Token parseOperatorName(Token token) { |
| 1866 assert(optional('operator', token)); | 1874 assert(optional('operator', token)); |
| 1867 if (isUserDefinableOperator(token.next.stringValue)) { | 1875 if (isUserDefinableOperator(token.next.stringValue)) { |
| 1868 Token operator = token; | 1876 Token operator = token; |
| 1869 token = token.next; | 1877 token = token.next; |
| 1870 listener.handleOperatorName(operator, token); | 1878 listener.handleOperatorName(operator, token); |
| 1871 return token.next; | 1879 return token.next; |
| 1872 } else { | 1880 } else { |
| 1873 return parseIdentifier(token, IdentifierContext.operatorName); | 1881 return parseIdentifier(token, IdentifierContext.operatorName); |
| 1874 } | 1882 } |
| 1875 } | 1883 } |
| 1876 | 1884 |
| 1877 Token parseFunction(Token token, Token getOrSet) { | 1885 Token parseFunction(Token token, Token getOrSet) { |
| 1886 Token beginToken = token; | |
| 1878 listener.beginFunction(token); | 1887 listener.beginFunction(token); |
| 1879 token = parseModifiers(token); | 1888 token = parseModifiers(token); |
| 1880 if (identical(getOrSet, token)) { | 1889 if (identical(getOrSet, token)) { |
| 1881 // get <name> => ... | 1890 // get <name> => ... |
| 1882 token = token.next; | 1891 token = token.next; |
| 1883 listener.handleNoType(token); | 1892 listener.handleNoType(token); |
| 1884 listener.beginFunctionName(token); | 1893 listener.beginFunctionName(token); |
| 1885 if (optional('operator', token)) { | 1894 if (optional('operator', token)) { |
| 1886 token = parseOperatorName(token); | 1895 token = parseOperatorName(token); |
| 1887 } else { | 1896 } else { |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 1902 listener.beginFunctionName(token); | 1911 listener.beginFunctionName(token); |
| 1903 if (optional('operator', token)) { | 1912 if (optional('operator', token)) { |
| 1904 token = parseOperatorName(token); | 1913 token = parseOperatorName(token); |
| 1905 } else { | 1914 } else { |
| 1906 token = | 1915 token = |
| 1907 parseIdentifier(token, IdentifierContext.localFunctionDeclaration); | 1916 parseIdentifier(token, IdentifierContext.localFunctionDeclaration); |
| 1908 } | 1917 } |
| 1909 } | 1918 } |
| 1910 token = parseQualifiedRestOpt( | 1919 token = parseQualifiedRestOpt( |
| 1911 token, IdentifierContext.localFunctionDeclarationContinuation); | 1920 token, IdentifierContext.localFunctionDeclarationContinuation); |
| 1912 listener.endFunctionName(token); | 1921 listener.endFunctionName(beginToken, token); |
| 1913 if (getOrSet == null) { | 1922 if (getOrSet == null) { |
| 1914 token = parseTypeVariablesOpt(token); | 1923 token = parseTypeVariablesOpt(token); |
| 1915 } else { | 1924 } else { |
| 1916 listener.handleNoTypeVariables(token); | 1925 listener.handleNoTypeVariables(token); |
| 1917 } | 1926 } |
| 1927 Token formalParametersToken = token; | |
| 1918 token = parseFormalParametersOpt(token); | 1928 token = parseFormalParametersOpt(token); |
| 1919 token = parseInitializersOpt(token); | 1929 token = parseInitializersOpt(token); |
| 1920 bool previousAsyncAwaitKeywordsEnabled = asyncAwaitKeywordsEnabled; | 1930 bool previousAsyncAwaitKeywordsEnabled = asyncAwaitKeywordsEnabled; |
| 1921 token = parseAsyncModifier(token); | 1931 token = parseAsyncModifier(token); |
| 1922 token = parseFunctionBody(token, false, true); | 1932 token = parseFunctionBody(token, false, true); |
| 1923 asyncAwaitKeywordsEnabled = previousAsyncAwaitKeywordsEnabled; | 1933 asyncAwaitKeywordsEnabled = previousAsyncAwaitKeywordsEnabled; |
| 1924 listener.endFunction(getOrSet, token); | 1934 listener.endFunction(getOrSet, formalParametersToken, token); |
| 1925 return token.next; | 1935 return token.next; |
| 1926 } | 1936 } |
| 1927 | 1937 |
| 1928 Token parseUnnamedFunction(Token token) { | 1938 Token parseUnnamedFunction(Token token) { |
| 1939 Token beginToken = token; | |
| 1929 listener.beginUnnamedFunction(token); | 1940 listener.beginUnnamedFunction(token); |
| 1930 token = parseFormalParameters(token); | 1941 token = parseFormalParameters(token); |
| 1931 bool previousAsyncAwaitKeywordsEnabled = asyncAwaitKeywordsEnabled; | 1942 bool previousAsyncAwaitKeywordsEnabled = asyncAwaitKeywordsEnabled; |
| 1932 token = parseAsyncModifier(token); | 1943 token = parseAsyncModifier(token); |
| 1933 bool isBlock = optional('{', token); | 1944 bool isBlock = optional('{', token); |
| 1934 token = parseFunctionBody(token, true, false); | 1945 token = parseFunctionBody(token, true, false); |
| 1935 asyncAwaitKeywordsEnabled = previousAsyncAwaitKeywordsEnabled; | 1946 asyncAwaitKeywordsEnabled = previousAsyncAwaitKeywordsEnabled; |
| 1936 listener.endUnnamedFunction(token); | 1947 listener.endUnnamedFunction(beginToken, token); |
| 1937 return isBlock ? token.next : token; | 1948 return isBlock ? token.next : token; |
| 1938 } | 1949 } |
| 1939 | 1950 |
| 1940 Token parseFunctionDeclaration(Token token) { | 1951 Token parseFunctionDeclaration(Token token) { |
| 1941 listener.beginFunctionDeclaration(token); | 1952 listener.beginFunctionDeclaration(token); |
| 1942 token = parseFunction(token, null); | 1953 token = parseFunction(token, null); |
| 1943 listener.endFunctionDeclaration(token); | 1954 listener.endFunctionDeclaration(token); |
| 1944 return token; | 1955 return token; |
| 1945 } | 1956 } |
| 1946 | 1957 |
| 1947 Token parseFunctionExpression(Token token) { | 1958 Token parseFunctionExpression(Token token) { |
| 1959 Token beginToken = token; | |
| 1948 listener.beginFunction(token); | 1960 listener.beginFunction(token); |
| 1949 listener.handleModifiers(0); | 1961 listener.handleModifiers(0); |
| 1950 token = parseReturnTypeOpt(token); | 1962 token = parseReturnTypeOpt(token); |
| 1951 listener.beginFunctionName(token); | 1963 listener.beginFunctionName(token); |
| 1952 token = parseIdentifier(token, IdentifierContext.functionExpressionName); | 1964 token = parseIdentifier(token, IdentifierContext.functionExpressionName); |
| 1953 listener.endFunctionName(token); | 1965 listener.endFunctionName(beginToken, token); |
| 1954 token = parseTypeVariablesOpt(token); | 1966 token = parseTypeVariablesOpt(token); |
| 1967 Token formalParametersToken = token; | |
| 1955 token = parseFormalParameters(token); | 1968 token = parseFormalParameters(token); |
| 1956 listener.handleNoInitializers(); | 1969 listener.handleNoInitializers(); |
| 1957 bool previousAsyncAwaitKeywordsEnabled = asyncAwaitKeywordsEnabled; | 1970 bool previousAsyncAwaitKeywordsEnabled = asyncAwaitKeywordsEnabled; |
| 1958 token = parseAsyncModifier(token); | 1971 token = parseAsyncModifier(token); |
| 1959 bool isBlock = optional('{', token); | 1972 bool isBlock = optional('{', token); |
| 1960 token = parseFunctionBody(token, true, false); | 1973 token = parseFunctionBody(token, true, false); |
| 1961 asyncAwaitKeywordsEnabled = previousAsyncAwaitKeywordsEnabled; | 1974 asyncAwaitKeywordsEnabled = previousAsyncAwaitKeywordsEnabled; |
| 1962 listener.endFunction(null, token); | 1975 listener.endFunction(null, formalParametersToken, token); |
| 1963 return isBlock ? token.next : token; | 1976 return isBlock ? token.next : token; |
| 1964 } | 1977 } |
| 1965 | 1978 |
| 1966 Token parseConstructorReference(Token token) { | 1979 Token parseConstructorReference(Token token) { |
| 1967 Token start = token; | 1980 Token start = token; |
| 1968 listener.beginConstructorReference(start); | 1981 listener.beginConstructorReference(start); |
| 1969 token = parseIdentifier(token, IdentifierContext.constructorReference); | 1982 token = parseIdentifier(token, IdentifierContext.constructorReference); |
| 1970 token = parseQualifiedRestOpt( | 1983 token = parseQualifiedRestOpt( |
| 1971 token, IdentifierContext.constructorReferenceContinuation); | 1984 token, IdentifierContext.constructorReferenceContinuation); |
| 1972 token = parseTypeArgumentsOpt(token); | 1985 token = parseTypeArgumentsOpt(token); |
| (...skipping 1605 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 3578 break; | 3591 break; |
| 3579 } | 3592 } |
| 3580 if (isRecoverable) { | 3593 if (isRecoverable) { |
| 3581 listener.handleRecoverableError(token, kind, arguments); | 3594 listener.handleRecoverableError(token, kind, arguments); |
| 3582 return null; | 3595 return null; |
| 3583 } else { | 3596 } else { |
| 3584 return listener.handleUnrecoverableError(token, kind, arguments)?.next; | 3597 return listener.handleUnrecoverableError(token, kind, arguments)?.next; |
| 3585 } | 3598 } |
| 3586 } | 3599 } |
| 3587 } | 3600 } |
| OLD | NEW |