Skip to content

Instantly share code, notes, and snippets.

@dburriss
Last active March 1, 2022 17:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dburriss/a16e19d3aba79594bd6508d8252aaa50 to your computer and use it in GitHub Desktop.
Save dburriss/a16e19d3aba79594bd6508d8252aaa50 to your computer and use it in GitHub Desktop.
Files with examples of grammar generated in rust-code-analysis project using tree-sitter along with logical lines of code counts
// Program
public class Person {
// ClassDeclaration
// Modifiers
// Public
// Class
// Identifier
// ClassBody
// LBRACE
private String name;
// FieldDeclaration
// Modifiers
// Private
// TypeIdentifier
// VariableDeclarator
// Identifier
// SEMI
public Person(String name){
// ConstructorDeclaration
// Modifiers
// Public
// Identifier
// FormalParameters
// LPAREN
// FormalParameter
// TypeIdentifier
// Identifier
// RPAREN
// ConstructorBody
// LBRACE
this.name = name; // +1
// ExpressionStatement
// AssignmentExpression
// FieldAccess
// This
// DOT
// Identifier
// EQ
// Identifier
// SEMI
// Comment
}
// RBRACE
public String getName() {
// MethodDeclaration
// Modifiers
// Public
// TypeIdentifier
// Identifier
// FormalParameters
// LPAREN
// RPAREN
// Block
// LBRACE
return name; // +1
// ReturnStatement
// Return
// Identifier
// SEMI
// Comment
}
// RBRACE
}
// RBRACE
// Program
int x = 10; // +1
// LocalVariableDeclaration
// IntegralType
// Int
// VariableDeclarator
// Identifier
// EQ
// DecimalIntegerLiteral
// SEMI
// Comment
x=+90; // +2 var + unary
// ExpressionStatement
// AssignmentExpression
// Identifier
// EQ
// UnaryExpression
// PLUS
// DecimalIntegerLiteral
// SEMI
// Comment
int y = x * 2; // +2 var + binary
// LocalVariableDeclaration
// IntegralType
// Int
// VariableDeclarator
// Identifier
// EQ
// BinaryExpression
// Identifier
// STAR
// DecimalIntegerLiteral
// SEMI
// Comment
IntFunction double = (n) -> n*2; // +3 var + lambda + binary
// LocalVariableDeclaration
// TypeIdentifier
// VariableDeclarator
// Identifier
// EQ
// LambdaExpression
// InferredParameters
// LPAREN
// Identifier
// RPAREN
// DASHGT
// BinaryExpression
// Identifier
// STAR
// DecimalIntegerLiteral
// SEMI
// Comment
int y2 = double(x); // +2 var + paren expression
// LocalVariableDeclaration
// IntegralType
// Int
// VariableDeclarator
// Identifier
// EQ
// Error
// FloatingPointType
// Double
// ParenthesizedExpression
// LPAREN
// Identifier
// RPAREN
// SEMI
// Comment
System.out.println("double " + x + " = " + y2); // +4 method + binary x 3
// ExpressionStatement
// MethodInvocation
// FieldAccess
// Identifier
// DOT
// Identifier
// DOT
// Identifier
// ArgumentList
// LPAREN
// BinaryExpression
// BinaryExpression
// BinaryExpression
// StringLiteral
// PLUS
// Identifier
// PLUS
// StringLiteral
// PLUS
// Identifier
// RPAREN
// SEMI
// Comment
String message = (x % 2) == 0 ? "Evenly done." : "Oddly done."; // +5 lloc : 1 var assignment + ternary + binary + param exp + binary
// LocalVariableDeclaration
// TypeIdentifier
// VariableDeclarator
// Identifier
// EQ
// TernaryExpression
// BinaryExpression
// ParenthesizedExpression
// LPAREN
// BinaryExpression
// Identifier
// PERCENT
// DecimalIntegerLiteral
// RPAREN
// EQEQ
// DecimalIntegerLiteral
// QMARK
// StringLiteral
// COLON
// StringLiteral
// SEMI
// Comment
Object done = (Runnable) () -> { System.out.println("Done!"); }; // +4 var + cast + lamda + method invoc
// LocalVariableDeclaration
// TypeIdentifier
// VariableDeclarator
// Identifier
// EQ
// CastExpression
// LPAREN
// TypeIdentifier
// RPAREN
// LambdaExpression
// FormalParameters
// LPAREN
// RPAREN
// DASHGT
// Block
// LBRACE
// ExpressionStatement
// MethodInvocation
// FieldAccess
// Identifier
// DOT
// Identifier
// DOT
// Identifier
// ArgumentList
// LPAREN
// StringLiteral
// RPAREN
// SEMI
// RBRACE
// SEMI
// Comment
String s = "string"; // +1
// LocalVariableDeclaration
// TypeIdentifier
// VariableDeclarator
// Identifier
// EQ
// StringLiteral
// SEMI
// Comment
boolean isS = (s instanceof String); // +3 var assignment + paren exp + instanceof
// LocalVariableDeclaration
// BooleanType
// VariableDeclarator
// Identifier
// EQ
// ParenthesizedExpression
// LPAREN
// InstanceofExpression
// Identifier
// Instanceof
// TypeIdentifier
// RPAREN
// SEMI
// Comment
done.run(); // +1 method invoc
// ExpressionStatement
// MethodInvocation
// Identifier
// DOT
// Identifier
// ArgumentList
// LPAREN
// RPAREN
// SEMI
// Comment
// Program
int max = 100;
// LocalVariableDeclaration
// IntegralType
// Int
// VariableDeclarator
// Identifier
// EQ
// DecimalIntegerLiteral
// SEMI
/*
Loop through and print
from: 0
to: max
*/
// Comment
for (int i = 0; i < max; i++) {
// ForStatement
// For
// LPAREN
// LocalVariableDeclaration
// IntegralType
// Int
// VariableDeclarator
// Identifier
// EQ
// DecimalIntegerLiteral
// SEMI
// BinaryExpression
// Identifier
// LT
// Identifier
// SEMI
// UpdateExpression
// Identifier
// PLUSPLUS
// RPAREN
// Block
// LBRACE
// Print the value
// Comment
System.out.println(i);
// ExpressionStatement
// MethodInvocation
// FieldAccess
// Identifier
// DOT
// Identifier
// DOT
// Identifier
// ArgumentList
// LPAREN
// Identifier
// RPAREN
// SEMI
}
// RBRACE
// Program
package com.company;
// PackageDeclaration
// Package
// ScopedIdentifier
// Identifier
// DOT
// Identifier
// SEMI
/**
* The HelloWorldApp class implements an application that
* simply prints \"Hello World!\" to standard output.
*/
// Comment
class HelloWorldApp {
// ClassDeclaration
// Class
// Identifier
// ClassBody
// LBRACE
public void main(String[] args) {
// MethodDeclaration
// Modifiers
// Public
// VoidType
// Identifier
// FormalParameters
// LPAREN
// FormalParameter
// ArrayType
// TypeIdentifier
// Dimensions
// LBRACK
// RBRACK
// Identifier
// RPAREN
// Block
// LBRACE
String message = args.length == 0 ? "Hello empty world" : "Hello world"; // +2 lloc : 1 var assignment + binary exp
// LocalVariableDeclaration
// TypeIdentifier
// VariableDeclarator
// Identifier
// EQ
// TernaryExpression
// BinaryExpression
// FieldAccess
// Identifier
// DOT
// Identifier
// EQEQ
// DecimalIntegerLiteral
// QMARK
// StringLiteral
// COLON
// StringLiteral
// SEMI
// Comment
System.out.println(message); // Display the string. +1 lloc
// ExpressionStatement
// MethodInvocation
// FieldAccess
// Identifier
// DOT
// Identifier
// DOT
// Identifier
// ArgumentList
// LPAREN
// Identifier
// RPAREN
// SEMI
// Comment
}
// RBRACE
}
// RBRACE
// Program
int i=0; // +1
// LocalVariableDeclaration
// IntegralType
// Int
// VariableDeclarator
// Identifier
// EQ
// DecimalIntegerLiteral
// SEMI
// Comment
while(i < 10) { // +2 ParenthesizedExpression + BinaryExpression
// WhileStatement
// While
// ParenthesizedExpression
// LPAREN
// BinaryExpression
// Identifier
// LT
// DecimalIntegerLiteral
// RPAREN
// Block
// LBRACE
// Comment
i++; // +1
// ExpressionStatement
// UpdateExpression
// Identifier
// PLUSPLUS
// SEMI
// Comment
System.out.println(i); // +1
// ExpressionStatement
// MethodInvocation
// FieldAccess
// Identifier
// DOT
// Identifier
// DOT
// Identifier
// ArgumentList
// LPAREN
// Identifier
// RPAREN
// SEMI
// Comment
}
// RBRACE
@dburriss
Copy link
Author

dburriss commented Mar 1, 2022

A note on this version (2). The LLOC counts are wrong. They are based on expression count rather than solely statements.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment