Split src into tokens.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| character(len=*), | intent(in) | :: | src |
source text |
||
| type(token_list_t), | intent(out) | :: | tokens |
its tokens, covering every byte |
subroutine tokenize(src, tokens) !! Split `src` into tokens. character(len=*), intent(in) :: src !! source text type(token_list_t), intent(out) :: tokens !! its tokens, covering every byte type(lexer_t) :: lx integer :: i, j, n, kind, sub logical :: at_start character :: c n = len(src) i = 1 if (n >= 3) then if (src(1:3) == achar(239)//achar(187)//achar(191)) then call add(tokens, lx, src, TK_BOM, 1, 3) i = 4 end if end if do while (i <= n) c = src(i:i) sub = 0 if (c == LF .or. c == CR) then j = i if (c == CR .and. peek(src, i + 1) == LF) j = i + 1 call add(tokens, lx, src, TK_NEWLINE, i, j) lx%line_start = .true. i = j + 1 cycle end if if (c == ' ' .or. c == TAB) then j = i do while (is_blank(peek(src, j + 1))) j = j + 1 end do call add(tokens, lx, src, TK_SPACE, i, j) i = j + 1 cycle end if at_start = lx%line_start lx%line_start = .false. if (at_start .and. is_preproc_start(src, i)) then call add(tokens, lx, src, TK_PREPROC, i, preproc_end(src, i)) i = tokens%tok(tokens%n)%last + 1 cycle end if select case (c) case ('!') j = line_end(src, i) call add(tokens, lx, src, TK_COMMENT, i, j, comment_kind(src(i:j))) case ('&') j = i if (at_start) then sub = CT_LEADING else sub = CT_TRAILING end if call add(tokens, lx, src, TK_CONT, i, j, sub) case ("'", '"') j = string_end(src, i, sub) call add(tokens, lx, src, TK_STRING, i, j, sub) case ('0':'9') call lex_number(src, i, j, kind) if (peek(src, j + 1) == '_' .and. is_quote(peek(src, j + 2))) then ! kind parameter prefix of a string: 1_'abc' j = string_end(src, j + 2, sub) kind = TK_STRING end if call add(tokens, lx, src, kind, i, j, sub) case ('.') if (is_digit(peek(src, i + 1))) then call lex_number(src, i, j, kind) call add(tokens, lx, src, kind, i, j) else if (dotop_at(src, i)) then j = dotop_end(src, i) call add(tokens, lx, src, TK_DOTOP, i, j) else j = i call add(tokens, lx, src, TK_OTHER, i, j) end if case ('a':'z', 'A':'Z', '_') ! a leading `_` is not standard, but appears in generated code j = name_end(src, i) kind = TK_NAME if (is_quote(peek(src, j + 1))) then if (j == i .and. index('bozBOZ', c) > 0) then kind = TK_BOZ j = string_end(src, j + 1, sub) else if (src(j:j) == '_') then kind = TK_STRING j = string_end(src, j + 1, sub) end if end if call add(tokens, lx, src, kind, i, j, sub) case default j = fypp_inline_end(src, i) if (j > 0) then call add(tokens, lx, src, TK_FYPP, i, j) else j = operator_end(src, i) if (j > 0) then call add(tokens, lx, src, TK_OP, i, j) else j = i call add(tokens, lx, src, TK_OTHER, i, j) end if end if end select i = j + 1 end do end subroutine tokenize