public bool BraceIsBlock(TokenType prevType) { if (prevType == TT["colon"]) { var parent = CurrentContext; if (parent == TokenContext.Types["b_stat"] || parent == TokenContext.Types["b_expr"]) { return(!parent.IsExpr); } } if (prevType == TT["_return"]) { return(LineBreak.IsMatch(Input.Slice(State.LastTokenEnd, State.Start))); } if (prevType == TT["_else"] || prevType == TT["semi"] || prevType == TT["eof"] || prevType == TT["parenR"]) { return(true); } if (prevType == TT["braceL"]) { return(CurrentContext == TokenContext.Types["b_stat"]); } return(!State.ExprAllowed); }
void readToken_plus_min(int code) { // '+-' var next = _input[_pos.Index + 1]; if (next == code) { if (next == 45 && !_inModule && _input.Get(_pos.Index + 2) == 62 && (_lastTokEnd.Index == 0 || LineBreak.IsMatch(_input.Substring(_lastTokEnd.Index, _pos - _lastTokEnd)))) { // A `-->` line comment SkipLineComment(3); SkipSpace(); NextToken(); return; } FinishOp(TokenType.IncDec, 2); } else if (next == 61) { FinishOp(TokenType.Assign, 2); } else { FinishOp(TokenType.PlusMin, 1); } }
bool BraceIsBlock(TokenType prevType) { var parent = CurContext(); if (parent == TokContext.FExpr || parent == TokContext.FStat) { return(true); } if (prevType == TokenType.Colon && (parent == TokContext.BStat || parent == TokContext.BExpr)) { return(!parent.IsExpression); } // The check for `tt.name && exprAllowed` detects whether we are // after a `yield` or `of` construct. See the `updateContext` for // `tt.name`. if (prevType == TokenType.Return || prevType == TokenType.Name && _exprAllowed) { return(LineBreak.IsMatch(_input.Substring(_lastTokEnd.Index, Start.Index - _lastTokEnd.Index))); } if (prevType == TokenType.Else || prevType == TokenType.Semi || prevType == TokenType.Eof || prevType == TokenType.ParenR || prevType == TokenType.Arrow) { return(true); } if (prevType == TokenType.BraceL) { return(parent == TokContext.BStat); } if (prevType == TokenType.Var || prevType == TokenType.Name) { return(false); } return(!_exprAllowed); }
public YAMLWriter(TextWriter tw, int initialdepth, int horizontalthresh, IDictionary <Type, bool> bbd) { Depth = new string(' ', initialdepth); HorizontalThresh = horizontalthresh; Lw = new LineWriter(tw, 0); BBD = bbd; VertWriter = new LeafWriter(this, VertNeedsQuotes, s => LineBreak.IsMatch(s)); HorizontalArrWriter = new LeafWriter(this, HArrNeedsQuotes, s => false); HorizontalDicWriter = new LeafWriter(this, HDicNeedsQuotes, s => false); }
AstThrow ParseThrowStatement(Position nodeStart) { Next(); if (LineBreak.IsMatch(_input.Substring(_lastTokEnd.Index, Start.Index - _lastTokEnd.Index))) { Raise(_lastTokEnd, "Illegal newline after throw"); } var argument = ParseExpression(); Semicolon(); return(new AstThrow(this, nodeStart, _lastTokEnd, argument)); }
private static void NameUpdateContext(Tokenizer tok, TokenType prevType) { tok.State.ExprAllowed = false; if (prevType == Types["_let"] || prevType == Types["_const"] || prevType == Types["_var"]) { if (LineBreak.IsMatch(tok.Input.Slice(tok.State.End))) { tok.State.ExprAllowed = true; } } }
// check 'async [no LineTerminator here] function' // - 'async /*foo*/ function' is OK. // - 'async /*\n*/ function' is invalid. bool IsAsyncFunction() { if (Type != TokenType.Name || Options.EcmaVersion < 8 || (string)Value != "async") { return(false); } var skip = SkipWhiteSpace.Match(_input, _pos.Index); var next = _pos.Index + skip.Groups[0].Length; return(!LineBreak.IsMatch(_input.Substring(_pos.Index, next - _pos.Index)) && _input.Length >= next + 8 && _input.AsSpan(next, 8).SequenceEqual("function") && (next + 8 == _input.Length || !IsIdentifierChar(_input.Get(next + 8)))); }
private Node ParseThrowStatement(Node node) { Next(); if (LineBreak.IsMatch(Input.Slice(State.LastTokenEnd, State.Start))) { Raise(State.LastTokenEnd, "Illegal newline after throw"); } node.Argument = ParseExpression(); Semicolon(); return(FinishNode(node, "ThrowStatement")); }
private TokenType ReadTokenPlusMin(int code) { var next = Input.CharCodeAt(State.Position + 1); if (next == code) { if (next == 45 && Input.CharCodeAt(State.Position + 2) == 62 && LineBreak.IsMatch(Input.Slice(State.LastTokenEnd, State.Position))) { SkipLineComment(3); SkipSpace(); return(NextToken()); } return(FinishOp(TT["incDec"], 2)); } return(next == 61 ? FinishOp(TT["assign"], 2) : FinishOp(TT["plusMin"], 1)); }
private TokenType ReadRegexp() { var start = State.Position; var escaped = false; var inClass = false; for (;;) { if (State.Position >= Input.Length) { Raise(start, "Unterminated regular expression"); } var ch = Input.CharCodeAt(State.Position); if (LineBreak.IsMatch(ch.ToString())) { Raise(start, "Unterminated regular expression"); } if (escaped) { escaped = false; } else { if (ch == '[') { inClass = true; } else if (ch == ']' && inClass) { inClass = false; } else if (ch == '/' && !inClass) { break; } escaped = ch == '\\'; } ++State.Position; } var content = Input.Slice(start, State.Position); ++State.Position; var mods = ReadWord1(); if (!string.IsNullOrEmpty(mods)) { var validFlags = new Regex("^[gmsiyu]*$"); if (!validFlags.IsMatch(mods)) { Raise(start, "Invalid regular expression flag"); } } return(FinishToken(TT["regexp"], new Node { Pattern = content, Flags = mods })); }
// Test whether a semicolon can be inserted at the current position. bool CanInsertSemicolon() { return(Type == TokenType.Eof || Type == TokenType.BraceR || LineBreak.IsMatch(_input.Substring(_lastTokEnd.Index, Start.Index - _lastTokEnd.Index))); }
void ReadRegexp() { var escaped = false; var inClass = false; var start = _pos; while (true) { if (_pos.Index >= _input.Length) { Raise(start, "Unterminated regular expression"); } var ch = _input[_pos.Index]; if (LineBreak.IsMatch(ch.ToString())) { Raise(start, "Unterminated regular expression"); } if (!escaped) { if (ch == '[') { inClass = true; } else if (ch == ']' && inClass) { inClass = false; } else if (ch == '/' && !inClass) { break; } escaped = ch == '\\'; } else { escaped = false; } _pos = _pos.Increment(1); } var content = _input.Substring(start.Index, _pos - start); _pos = _pos.Increment(1); // Need to use `readWord1` because '\uXXXX' sequences are allowed // here (don't ask). var mods = ReadWord1(); if (!string.IsNullOrEmpty(mods)) { var validFlags = new Regex("^[gim]*$"); if (Options.EcmaVersion >= 6) { validFlags = new Regex("^[gimuy]*$"); } if (!validFlags.IsMatch(mods)) { Raise(start, "Invalid regular expression flag"); } } FinishToken(TokenType.Regexp, new RegExp { Pattern = content, Flags = RegExp.String2Flags(mods) }); }