private CommentGroup CloseLastComments() { Debug.Assert(lastComments.Count > 0); var result = new CommentGroup(source, lastComments.ToArray()); lastComments.Clear(); return(result); }
public static CommentGroup ToModel(this CommentGroupViewModel viewModel) { if (viewModel != null) { CommentGroup model = new CommentGroup(); model.Id = viewModel.Id; model.Name = viewModel.Name; return(model); } return(null); }
public static CommentGroupViewModel ToViewModel(this CommentGroup model) { if (model != null) { CommentGroupViewModel viewModel = new CommentGroupViewModel(); viewModel.Id = model.Id; viewModel.Name = model.Name; return(viewModel); } return(null); }
/// <summary> /// Scan returns the next token from the underlying scanner. /// If a token has been unscanned then read that instead. /// In the process if collects any comment groups encountered /// and remembers the last lead and line comments /// </summary> /// <returns></returns> private void Scan() { // If we have a token on the buffer, then return it if (_n != 0) { _n = 0; return; } // Otherwise consume the next token from the scanner // and save it to the buffer in case we // unscan later var prev = _token; _token = _scanner.Scan(); if (_token.Type == TokenType.COMMENT) { CommentGroup comment = null; int endLine; if (_token.Pos.Line == prev.Pos.Line) { // Comment is on the same line as the previous token; // it cannot be a lead comment, but may be a line comment comment = ConsumeCommentGroup(0, out endLine); if (_token.Pos.Line != endLine) { // The next token is on a different line, thus // the last comment group is a line comment _lineComment = comment; } } // Consume successor comments, if any endLine = -1; if (_token.Type == TokenType.COMMENT) { comment = ConsumeCommentGroup(1, out endLine); } if (endLine + 1 == _token.Pos.Line && _token.Type != TokenType.RBRACE) { if (_token.Type != TokenType.RBRACE && _token.Type != TokenType.RBRACK) { // The next token is following on the line immediately // after the comment group. Thus the last comment group // is a lead comment. _leadComment = comment; } } } }
private CommentGroup ConsumeCommentGroup(int n, out int endLine) { var list = new List <Comment>(); endLine = _token.Pos.Line; while (_token.Type == TokenType.COMMENT && _token.Pos.Line <= endLine + n) { list.Add(ConsumeComment(out endLine)); } var group = new CommentGroup(list); _comments.Add(group); return(group); }
private CommentGroup?GetLineComment(Token t) { var comment = NextPrimitive(); if (comment.Type == TokenType.LineComment && comment.Span.Start.Line == t.Span.End.Line) { // Same line, belongs to the token var group = new CommentGroup(source, new Token[] { comment }); return(group); } else { // We can safely step back, as no comments were eaten PrevPrimitive(); return(null); } }
public void Migrate() { var commentGroups = SourceDbContext.CommentGroups.ToList(); foreach (var commentGroup in commentGroups) { var newEntity = new CommentGroup() { Id = _commentGroupKeyMapper.MapKey(commentGroup.Id), PostDate = commentGroup.PostDate }; TargetDbContext.CommentGroups.Add(newEntity); } TargetDbContext.SaveChanges(); }
private (CommentGroup comments, int endline) ConsumeCommentGroup(int n) { var list = new slice <Comment>(); var endline = tok.Pos.Line; while (tok.Type == TokenType.COMMENT && tok.Pos.Line <= endline + n) { Comment comment; (comment, endline) = ConsumeComment(); list = list.Append(comment); } // add comment group to the comments list var comments = new CommentGroup { List = list }; this.comments = this.comments.Append(comments); return(comments, endline); }
/// listType parses a list type and returns a ListType AST private ListType ListType() { try { // we assume that the currently scanned token is a LBRACK var l = new ListType { Lbrack = tok.Pos, }; var needComma = false; for (;;) { var tok = Scan(); if (needComma) { if (tok.Type == TokenType.COMMA || tok.Type == TokenType.RBRACK) { // Do nothing } else { throw new PosErrorException(tok.Pos, $"error parsing list, expected comma or list end, got: {tok.Type}"); } } if (tok.Type == TokenType.BOOL || tok.Type == TokenType.NUMBER || tok.Type == TokenType.FLOAT || tok.Type == TokenType.STRING || tok.Type == TokenType.HEREDOC) { var node = LiteralType(); // If there is a lead comment, apply it if (leadComment != null) { node.LeadComment = leadComment; leadComment = null; } l.Add(node); needComma = true; } else if (tok.Type == TokenType.COMMA) { // get next list item or we are at the end // do a look-ahead for line comment Scan(); if (lineComment != null && l.List.Length > 0) { var lit = l.List[l.List.Length - 1] as LiteralType; if (lit != null) { lit.LineComment = lineComment; l.List[l.List.Length - 1] = lit; lineComment = null; } } Unscan(); needComma = false; continue; } else if (tok.Type == TokenType.LBRACE) { // Looks like a nested object, so parse it out ObjectType node; try { node = ObjectType(); } catch (Exception ex) { throw new PosErrorException(tok.Pos, "error while trying to parse object within list", ex); } l.Add(node); needComma = true; } else if (tok.Type == TokenType.LBRACK) { ListType node; try { node = ListType(); } catch (Exception ex) { throw new PosErrorException(tok.Pos, "error while trying to parse list within list", ex); } l.Add(node); } else if (tok.Type == TokenType.RBRACK) { // finished l.Rbrack = tok.Pos; return(l); } else { throw new PosErrorException(tok.Pos, $"unexpected token while parsing list: {tok.Type}"); } } } finally { //defer un(trace(p, "ParseListType")) } }
public async Task <int> UpdateAsync(CommentGroup updatedObject) { _context.CommentGroups.Update(updatedObject); return(await _context.SaveChangesAsync()); }
private ListType ParseListType(out string parseError) { // We assume that the current scanned token is a LBRACK var lBrackPos = _token.Pos; var items = new List <INode>(); var needComma = false; while (true) { Scan(); if (needComma) { if (_token.Type != TokenType.COMMA && _token.Type != TokenType.RBRACK) { parseError = string.Format("Error parsing list. Expected comma or list end, got: {0}", _token.Type); return(null); } } switch (_token.Type) { case TokenType.NUMBER: case TokenType.FLOAT: case TokenType.STRING: case TokenType.HEREDOC: var literal = ParseLiteralType(out parseError); if (!string.IsNullOrEmpty(parseError)) { return(null); } // If there's a lead comment, apply it if (_leadComment != null) { literal.LeadComment = _leadComment; // And consume it _leadComment = null; } items.Add(literal); needComma = true; break; case TokenType.COMMA: // Get next list item, or we are at the end // Do a lookahead for possible line comment Scan(); // Did we just read a comment? if (_lineComment != null && items.Count > 0) { var lastLiteral = items.Last() as LiteralType; if (lastLiteral != null) { lastLiteral.LineComment = _lineComment; _lineComment = null; } } Unscan(); needComma = false; break; case TokenType.LBRACE: // Looks like a nested object, so parse it out var objectType = ParseObjectType(out parseError); if (!string.IsNullOrEmpty(parseError)) { return(null); } items.Add(objectType); needComma = true; break; case TokenType.RBRACK: // Finished; var rBrackPos = _token.Pos; parseError = null; return(new ListType(lBrackPos, items, rBrackPos)); case TokenType.BOOL: case TokenType.LBRACK: // Not supported by upstream implementation yet default: parseError = string.Format("Unexpected token while parsing list: {0}", _token.Type); return(null); } } }
/// scan returns the next token from the underlying scanner. If a token has /// been unscanned then read that instead. In the process, it collects any /// comment groups encountered, and remembers the last lead and line comments. private Token Scan() { // If we have a token on the buffer, then return it. if (n != 0) { n = 0; return(tok); } // Otherwise read the next token from the scanner and Save it to the buffer // in case we unscan later. var prev = tok; tok = sc.Scan(); if (tok.Type == TokenType.COMMENT) { CommentGroup comment = null; int endline; // fmt.Printf("p.tok.Pos.Line = %+v prev: %d endline %d \n", // p.tok.Pos.Line, prev.Pos.Line, endline) if (tok.Pos.Line == prev.Pos.Line) { // The comment is on same line as the previous token; it // cannot be a lead comment but may be a line comment. (comment, endline) = ConsumeCommentGroup(0); if (tok.Pos.Line != endline) { // The next token is on a different line, thus // the last comment group is a line comment. lineComment = comment; } } // consume successor comments, if any endline = -1; while (tok.Type == TokenType.COMMENT) { (comment, endline) = ConsumeCommentGroup(1); } if (endline + 1 == tok.Pos.Line && tok.Type != TokenType.RBRACE) { switch (tok.Type) { // Do not count for these cases case TokenType.RBRACE: break; case TokenType.RBRACK: break; default: // The next token is following on the line immediately after the // comment group, thus the last comment group is a lead comment. leadComment = comment; break; } } } return(tok); }
internal ObjectItem ParseObjectItem(out string parseError) { var keys = ParseObjectKey(out parseError); if (keys.Length > 0 && parseError == ErrEofToken) { // We ignore eof token here since it is an error if we // didin't receive a value (but we did receive a key) // for the item parseError = null; } if (keys.Length > 0 && parseError != null && _token.Type == TokenType.RBRACE) { // This is a strange boolean statement, but what it means is: // We have keys with no value, and we're likely in an object // (since RBrace ends an object). For this, we set err to nil so // we continue and get the error below of having the wrong value // type. parseError = null; // Reset the token type so we don't think it completed fine. See // objectType which uses p.tok.Type to check if we're done with // the object. _token = new Token(TokenType.EOF, _token.Pos, null, _token.IsJson); } if (parseError != null) { return(null); } var leadComment = _leadComment; _leadComment = null; Pos assign; INode val; switch (_token.Type) { case TokenType.ASSIGN: assign = _token.Pos; val = ParseObject(out parseError); if (parseError != null) { return(null); } break; case TokenType.LBRACE: assign = default(Pos); val = ParseObjectType(out parseError); if (parseError != null) { return(null); } break; default: parseError = string.Format("key '{0}' expected start of object ('{') or assignment ('=')", string.Join(" ", keys.Select(k => k.Token.Text))); return(null); } // Do a look ahead for a line comment Scan(); CommentGroup lineComment = null; if (keys.Length > 0 && val.Pos.Line == keys[0].Pos.Line) { lineComment = _lineComment; _lineComment = null; } Unscan(); return(new ObjectItem(keys, assign, val, leadComment, lineComment)); }
public async Task <CommentGroupViewModel> Get(int id) { CommentGroup commentGroup = await _service.getAsync(id); return(commentGroup.ToViewModel()); }
public LiteralType(Token token, CommentGroup leadComment, CommentGroup lineComment) { _token = token; _leadComment = leadComment; _lineComment = lineComment; }
public async Task <int> CreateAsync(CommentGroup newObject) { _context.CommentGroups.Add(newObject); return(await _context.SaveChangesAsync()); }
/// objectItem parses a single object item public ObjectItem ObjectItem() { try { var keys = slice <ObjectKey> .Empty; try { keys = ObjectKey(); } catch (ErrEofTokenException) when(keys.Length > 0) { // We ignore eof token here since it is an error if we didn't // receive a value (but we did receive a key) for the item. //err = nil; } catch (Exception) when(keys.Length > 0 && tok.Type == TokenType.RBRACE) { // This is a strange boolean statement, but what it means is: // We have keys with no value, and we're likely in an object // (since RBrace ends an object). For this, we set err to nil so // we continue and get the error below of having the wrong value // type. //err = nil // Reset the token type so we don't think it completed fine. See // objectType which uses p.tok.Type to check if we're done with // the object. tok.Type = TokenType.EOF; } var o = new ObjectItem { Keys = keys, }; if (leadComment != null) { o.LeadComment = leadComment; leadComment = null; } switch (tok.Type) { case TokenType.ASSIGN: o.Assign = tok.Pos; o.Val = Object(); break; case TokenType.LBRACE: o.Val = ObjectType(); break; default: var keyStr = slice.Make <string>(0, keys.Length); foreach (var k in keys) { keyStr = keyStr.Append(k.Token.Text); } throw new PosErrorException(tok.Pos, $"key '{string.Join(" ", keyStr)}' expected start of object ('{{') or assignment ('=')"); } // do a look-ahead for line comment Scan(); if (keys.Length > 0 && o.Val.Pos().Line == keys[0].Pos().Line&& lineComment != null) { o.LineComment = lineComment; lineComment = null; } Unscan(); return(o); } finally { // defer un(trace(p, "ParseObjectItem")) } }