Search in sources :

Example 6 with CodeReader

use of org.sonar.channel.CodeReader in project sonar-web by SonarSource.

the class ElementTokenizer method parseToken.

private void parseToken(Node node) {
    TagNode element = (TagNode) node;
    CodeReader codeReader = new CodeReader(node.getCode());
    ParseMode mode = ParseMode.BEFORE_NODE_NAME;
    for (int ch = codeReader.peek(); ch != -1; ch = codeReader.peek()) {
        // handle white space
        if (Character.isWhitespace(ch)) {
            codeReader.pop();
            continue;
        }
        // handle special characters
        switch(ch) {
            case '=':
                mode = ParseMode.BEFORE_ATTRIBUTE_VALUE;
                codeReader.pop();
                continue;
            case '<':
                nestedTag(element, codeReader, mode);
                continue;
            case '>':
            case '/':
            case '%':
            case '@':
                codeReader.pop();
                continue;
            default:
                break;
        }
        mode = parseToken(mode, codeReader, element);
    }
}
Also used : CodeReader(org.sonar.channel.CodeReader) TagNode(org.sonar.plugins.web.node.TagNode)

Example 7 with CodeReader

use of org.sonar.channel.CodeReader in project sonar-web by SonarSource.

the class PageLexer method parse.

/**
 * Parse the input into a list of tokens, with parent/child relations between the tokens.
 */
public List<Node> parse(Reader reader) {
    // CodeReader reads the file stream
    CodeReader codeReader = new CodeReader(reader);
    // ArrayList collects the nodes
    List<Node> nodeList = new ArrayList<>();
    // ChannelDispatcher manages the tokenizers
    ChannelDispatcher<List<Node>> channelDispatcher = ChannelDispatcher.builder().addChannels((Channel[]) tokenizers.toArray(new Channel[tokenizers.size()])).build();
    channelDispatcher.consume(codeReader, nodeList);
    createNodeHierarchy(nodeList);
    return nodeList;
}
Also used : TagNode(org.sonar.plugins.web.node.TagNode) Node(org.sonar.plugins.web.node.Node) Channel(org.sonar.channel.Channel) ArrayList(java.util.ArrayList) CodeReader(org.sonar.channel.CodeReader) List(java.util.List) ArrayList(java.util.ArrayList)

Example 8 with CodeReader

use of org.sonar.channel.CodeReader in project sonarqube by SonarSource.

the class BlackHoleTokenChannelTest method shouldConsume.

@Test
public void shouldConsume() {
    BlackHoleTokenChannel channel = new BlackHoleTokenChannel("ABC");
    TokenQueue output = mock(TokenQueue.class);
    CodeReader codeReader = new CodeReader("ABCD");
    assertThat(channel.consume(codeReader, output)).isTrue();
    assertThat(codeReader.getLinePosition()).isEqualTo(1);
    assertThat(codeReader.getColumnPosition()).isEqualTo(3);
    verifyZeroInteractions(output);
}
Also used : CodeReader(org.sonar.channel.CodeReader) Test(org.junit.Test)

Example 9 with CodeReader

use of org.sonar.channel.CodeReader in project sonarqube by SonarSource.

the class TokenChannelTest method shouldNotConsume.

@Test
public void shouldNotConsume() {
    TokenChannel channel = new TokenChannel("ABC");
    TokenQueue output = mock(TokenQueue.class);
    CodeReader codeReader = new CodeReader("123");
    assertThat(channel.consume(new CodeReader("123"), output), is(false));
    verifyZeroInteractions(output);
    assertThat(codeReader.getLinePosition(), is(1));
    assertThat(codeReader.getColumnPosition(), is(0));
}
Also used : CodeReader(org.sonar.channel.CodeReader) Test(org.junit.Test)

Example 10 with CodeReader

use of org.sonar.channel.CodeReader in project sonarqube by SonarSource.

the class TokenChannelTest method shouldCorrectlyDeterminePositionWhenTokenSpansMultipleLines.

@Test
public void shouldCorrectlyDeterminePositionWhenTokenSpansMultipleLines() {
    TokenChannel channel = new TokenChannel("AB\nC");
    TokenQueue output = mock(TokenQueue.class);
    CodeReader codeReader = new CodeReader("AB\nCD");
    assertThat(channel.consume(codeReader, output), is(true));
    ArgumentCaptor<Token> token = ArgumentCaptor.forClass(Token.class);
    verify(output).add(token.capture());
    assertThat(token.getValue(), is(new Token("AB\nC", 1, 0)));
    verifyNoMoreInteractions(output);
    assertThat(codeReader.getLinePosition(), is(2));
    assertThat(codeReader.getColumnPosition(), is(1));
}
Also used : CodeReader(org.sonar.channel.CodeReader) Test(org.junit.Test)

Aggregations

CodeReader (org.sonar.channel.CodeReader)10 Test (org.junit.Test)6 TagNode (org.sonar.plugins.web.node.TagNode)3 ArrayList (java.util.ArrayList)2 Node (org.sonar.plugins.web.node.Node)2 List (java.util.List)1 Channel (org.sonar.channel.Channel)1 DuplicationsException (org.sonar.duplications.DuplicationsException)1 CommentNode (org.sonar.plugins.web.node.CommentNode)1 DirectiveNode (org.sonar.plugins.web.node.DirectiveNode)1 TextNode (org.sonar.plugins.web.node.TextNode)1